DetailsView with dropdownlist

(ASP.NET)


Hi,
In this article i placed drodownlist in insert mode and edit mode of the details view.

.aspx page

 <div>
        <asp:DetailsView ID="dtlsvw_products"  OnItemUpdating="dtlsvw_products_ItemUpdating" AutoGenerateInsertButton="True"  AutoGenerateEditButton="True"  AllowPaging="True" DataSourceID="sqlds_prodtype"   OnDataBound="dtlsvw_products_DataBound" AutoGenerateRows="False"   DataKeyNames="CategoryID" runat="server" >
         <Fields>
            <asp:BoundField DataFormatString="{0:##,###.00}" HeaderText="Product ID" DataField="ProductID"  />
             <asp:BoundField HeaderText="Product Name" DataField="ProductName"  />
             <asp:TemplateField  HeaderText="Category">
                 <EditItemTemplate>
                     <asp:DropDownList    DataValueField="CategoryID" DataTextField="CategoryName" ID="drp_editcategory" runat="server">

                     </asp:DropDownList>
                 </EditItemTemplate>
                 <InsertItemTemplate>
                     <asp:DropDownList    DataValueField="CategoryID" DataTextField="CategoryName" ID="drp_insertcategory" runat="server">

                     </asp:DropDownList>
                 </InsertItemTemplate>
                 <ItemTemplate>
                     <asp:Label ID="lbl_category"  runat="server" ></asp:Label>
                 </ItemTemplate>
             </asp:TemplateField>
               <asp:BoundField ReadOnly="true" HeaderText="Category ID" DataField="CategoryID" />
         </Fields>
        </asp:DetailsView>
          <asp:SqlDataSource ID="sqlds_prodtype" runat="server" ConnectionString="<%$ ConnectionStrings:Northwindconstring %>"
            SelectCommand="SELECT * FROM Products"
              UpdateCommand="UPDATE [Products] SET [ProductName] = @ProductName WHERE [ProductID] =@ProductID"
              InsertCommand="INSERT INTO [Products] (ProductName,QuantityPerUnit) VALUES(@ProductName,@CategoryName)"
               >
           
              <UpdateParameters>
                  <asp:Parameter Name="ProductName" Type="String" /> 
                <asp:Parameter Name="ProductID" Type="String" /> 
              </UpdateParameters>
              <InsertParameters>
                  <asp:Parameter Name="ProductName" Type="String" />
                 <asp:ControlParameter ControlID="dtlsvw_products$drp_insertcategory" PropertyName="SelectedValue" Type="String" Name="CategoryName" />
              </InsertParameters>
              </asp:SqlDataSource>

    </div>


code behind:-
  protected void dtlsvw_products_DataBound(object sender, EventArgs e)
        {
            try
            {
                string categoryid = null;

                if (dtlsvw_products.CurrentMode == DetailsViewMode.ReadOnly)
                {
                    if (dtlsvw_products.DataItem != null)
                    {
                      
                        categoryid = DataBinder.Eval(dtlsvw_products.DataItem, "CategoryID").ToString();
                        ViewState["categoryid"] = categoryid;
                        Label lbl = dtlsvw_products.FindControl("lbl_category") as Label;
                        bindLabel(lbl, categoryid);
                    }
                }

            

                else  if (dtlsvw_products.CurrentMode == DetailsViewMode.Insert)
                {

                    if (ViewState["categoryid"] != null)
                        categoryid = Convert.ToString(ViewState["categoryid"]);
                    DropDownList drp = dtlsvw_products.FindControl("drp_insertcategory") as DropDownList;

                    binddrpdownList(drp, categoryid);
                }
             
                else if (dtlsvw_products.CurrentMode == DetailsViewMode.Edit)
                {
                   if(ViewState["categoryid"] != null)
                       categoryid = Convert.ToString(ViewState["categoryid"]);

                    DropDownList drp = dtlsvw_products.FindControl("drp_editcategory") as DropDownList;
                    binddrpdownList(drp, categoryid);
                }

             
            }
            catch (Exception)
            { }
        }

        private void binddrpdownList(DropDownList drpdwnlst, string categoryid)
        {
            try
            {
                SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwindconstring"].ToString());

                string query = "Select * from Categories where CategoryID = " + categoryid;

                DataSet ds = new DataSet();

                SqlDataAdapter sqladp = new SqlDataAdapter(query, sqlcon);

                sqladp.Fill(ds);

                drpdwnlst.DataSource = ds;
                drpdwnlst.DataBind();


            }
            catch (Exception)
            { }
        }

        private void bindLabel(Label lbl, string categoryid)
        {
            try
            {
                SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["Northwindconstring"].ToString());

                string query = "Select CategoryName from Categories where CategoryID=" + categoryid;

                SqlCommand cmd = new SqlCommand(query, sqlcon);
                sqlcon.Open();
                string categoryname = (string)cmd.ExecuteScalar();
                sqlcon.Close();
                lbl.Text = categoryname;

              
            }
            catch (Exception)
            { }
        }



Image

Ratings


Average Rating: 0.00 by 0 users
Giri Prasad
6/28/2013
6/28/2013
Download PDF

Comments