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" 
                 DefaultMode="Insert" Height="50px" Width="125px">
<asp:TemplateField HeaderText="Category">
<asp:DropDownList ID="ddlCategory" runat="server" AutoPostBack="true"
                  DataTextField="Category" DataValueField="Category" 
        OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged" >
<asp:SqlDataSource ID="SqlDataSourceCategory" runat="server" 
ConnectionString="<%$ ConnectionStrings:dvConnectionString %>"
SelectCommand="SELECT DISTINCT [Category] FROM [Products]">

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

<asp:TemplateField HeaderText="Description">
<asp:TextBox runat="server" ID="txtDes" Text='<%#Bind("Des") %>'>
<asp:CommandField ShowInsertButton="True" />
<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" >
<asp:Parameter Name="Category" />
<asp:Parameter Name="Product" />
<asp:Parameter Name="Des" />

C# code behind
protected void ddlCategory_SelectedIndexChanged
                   (object sender, EventArgs e)
 DropDownList ddlProducts = 

        if (ddlProducts != null)

protected void SqlDataSourceProd_Selecting
    (object sender, SqlDataSourceSelectingEventArgs e)
  DropDownList ddlCategory = 

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

protected void SqlDataSourceDetails_Inserting
       (object sender, SqlDataSourceCommandEventArgs e)
 DropDownList ddlCat = 
 e.Command.Parameters["@Category"].Value = ddlCat.SelectedValue;
 DropDownList ddlProd = 
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
    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 = 
    End If
End Sub

Download the code from here

If you like this post than join us or share


Anonymous said...

Very Nice example with source code

Jen said...

The blog is helpfull...
visit also asp.net [c#]

David Fowler 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

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 :)

Rem said...

nice blog...
visit also asp.net example

Shakti Singh Dulawat said...

This is very nice example mention my you, this is helpful for programming logic.

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