Populate DropdownList In SelectedIndexChanged DetailsView GridView

In this example i am explaining how to Bind Populate Second DropDownList In SelectedIndexChanged Event Based On Selection Of Other First Dropdown In Detailsview Gridview ASP.NET and save the record in database, in this example i've added two DropDowns in DetailsView control using TemplateField and InsertItemTemplate

The Second DropDown (ddlProducts) is getting populated based on Category selected in Category DropDown , For this i've used SelectedIndexChanged event and findControl method to find the control and sqldataSource is used as datasource for the dropdowns and DetailView


HTML source of the aspx page is
<asp:DetailsView ID="dView" runat="server" AutoGenerateRows="False" 
                 DataSourceID="SqlDataSourceDetails"
                 DefaultMode="Insert" Height="50px" Width="125px">
<Fields>
<asp:TemplateField HeaderText="Category">
<InsertItemTemplate>
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true"
                  DataSourceID="SqlDataSourceCategory" 
                  DataTextField="Category" DataValueField="Category" 
        OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged" >
</asp:DropDownList>  
<asp:SqlDataSource ID="SqlDataSourceCategory" runat="server" 
ConnectionString="<%$ ConnectionStrings:dvConnectionString %>"
SelectCommand="SELECT DISTINCT [Category] FROM [Products]">
</asp:SqlDataSource>
</InsertItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Product">
<InsertItemTemplate>
<asp:DropDownList ID="ddlProducts" runat="server" 
                  DataSourceID="SqlDataSourceProd"
                  DataTextField="Product" DataValueField="Product">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceProd" runat="server" 
ConnectionString="<%$ ConnectionStrings:dvConnectionString %>"
SelectCommand="SELECT [Product] FROM [Products] 
WHERE ([Category] = @Category)" 
OnSelecting="SqlDataSourceProd_Selecting">
<SelectParameters>
<asp:Parameter Name="Category" Type="String" />
</SelectParameters>
</asp:SqlDataSource> 
</InsertItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Description">
<InsertItemTemplate>
<asp:TextBox runat="server" ID="txtDes" Text='<%#Bind("Des") %>'>
</asp:TextBox>
</InsertItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
        
<asp:SqlDataSource ID="SqlDataSourceDetails" runat="server" 
ConnectionString="<%$ ConnectionStrings:dvConnectionString %>"
SelectCommand="SELECT * FROM [Products]" 
InsertCommand="INSERT INTO Products(Category, Product, Des) 
               VALUES (@Category,@Product,@Des)" 
OnInserting="SqlDataSourceDetails_Inserting" >
<InsertParameters>
<asp:Parameter Name="Category" />
<asp:Parameter Name="Product" />
<asp:Parameter Name="Des" />
</InsertParameters>
</asp:SqlDataSource>

C# code behind
protected void ddlCategory_SelectedIndexChanged
                   (object sender, EventArgs e)
{
 DropDownList ddlProducts = 
     (DropDownList)dView.FindControl("ddlProducts");

        if (ddlProducts != null)
        {
            ddlProducts.DataBind();
        }
}

protected void SqlDataSourceProd_Selecting
    (object sender, SqlDataSourceSelectingEventArgs e)
{
  DropDownList ddlCategory = 
      (DropDownList)dView.FindControl("ddlCategory");

        if (ddlCategory != null)
        {
            e.Command.Parameters["@Category"].Value = ddlCategory.SelectedValue;
        }
}

protected void SqlDataSourceDetails_Inserting
       (object sender, SqlDataSourceCommandEventArgs e)
{
 DropDownList ddlCat = 
         (DropDownList)dView.FindControl("ddlCategory");
 e.Command.Parameters["@Category"].Value = ddlCat.SelectedValue;
 DropDownList ddlProd = 
     (DropDownList)dView.FindControl("ddlProducts");
e.Command.Parameters["@Product"].Value = ddlProd.SelectedValue;
}

VB.NET Code behind
Protected Sub ddlCategory_SelectedIndexChanged
(ByVal sender As Object, ByVal e As EventArgs)

Dim ddlProducts As DropDownList = 
DirectCast(dView.FindControl("ddlProducts"), DropDownList)
    
    If ddlProducts IsNot Nothing Then
        ddlProducts.DataBind()
    End If
End Sub

Protected Sub SqlDataSourceProd_Selecting
(ByVal sender As Object, 
ByVal e As SqlDataSourceSelectingEventArgs)

Dim ddlCategory As DropDownList = 
DirectCast(dView.FindControl("ddlCategory"), DropDownList)
    
    If ddlCategory IsNot Nothing Then
    e.Command.Parameters("@Category").Value = 
                     ddlCategory.SelectedValue
    End If
End Sub

Download the code from here







If you like this post than join us or share

13 comments:

Anonymous said...

Very Nice example with source code
Thanx


Anonymous said...

You can achieve the same thing without writing any code in the code behind. I made a modified version of your sample with the fix. Check it out
here


Rohit Saxena said...

Gr8 man..I found your article on the Article of the day list of Official Microsoft ASP .Net site.
Keep the good work going up :)


Shakti Singh Dulawat said...

This is very nice example mention my you, this is helpful for programming logic.
Thanks
Shakti
www.nextmvp.blogspot.com


Anonymous said...

how is the approach of this code in vb code?


Anonymous said...

It is rather interesting for me to read that article. Thank you for it. I like such topics and anything connected to this matter. I definitely want to read a bit more soon.


Anonymous said...

Don't stop posting such articles. I love to read articles like this. BTW add some pics :)


James said...

Thanks for clarifying this - I'm using a variation on this code now for nearly all my apps.


Anonymous said...

Genial brief and this enter helped me alot in my college assignement. Thanks you seeking your information.


Anonymous said...

Nice fill someone in on and this mail helped me alot in my college assignement. Gratefulness you for your information.


Anonymous said...

cheers!!!!!! took me 4 days trying to figure this one out,i could have gone even longer...cheers!!!!


Sergio Palmeros said...

Thanks for the article. The only problem I have now is when I applied it to my application it only works for the first selection on the first dropdown, but if I change the first selection the second dropdown doesn't obey the command anymore. Please help. Thanks again.


John said...

Thanks, this is very helpful.


Find More Articles