Crisis with a dynamically created drop down list
-
Here's the deal! I'm creating two drop down lists dynamically in a GridView control and populating the second dropdownlist(let's call it ddl2 from now on) on the basis of the selection on the first dropdownlist control. The crisis is that, for multiple rows on the gridview the ddl2 starts appending values to itself and therefore ends up having repeats of the data it should originally hold. How do I stop this repeat from happening? My code is as below.
<asp:GridView ID="grd" runat="server" OnRowDataBound="grd_RowDataBound"> <Columns> <asp:TemplateField HeaderText="names"> <ItemTemplate> <asp:DropDownList ID="name" runat="server" OnSelectedIndexChanged="onSelectChangeddl"></asp:DropDownList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="age"> <ItemTemplate> <asp:DropDownList ID="age" runat="server" AutoPostBack="true"> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
In c# code, i am trying to change the values of the second drop down list based on the value selected in the first. I am doing this as below:
foreach (GridViewRow row in grd.Rows) { DropDownList ddl1 = (DropDownList)row.FindControl("name"); DropDownList ddl = (DropDownList)row.FindControl("age"); if (ddl.SelectedValue.ToString() != "---Select---") { } else { ddl.Items.Clear(); } SqlCommand SPComm = new SqlCommand("someStoredProcedure", myConn); SPComm.CommandType = CommandType.StoredProcedure; SqlParameter param = SPComm.Parameters.Add("@something", SqlDbType.NVarChar, 50); param.Value = something; dr = SPComm.ExecuteReader(); //dr is sqlDataReader int i = 0; while (dr.Read()) { ddl.Items.Insert(i, dr.GetString(0)); } dr.Close(); ddl.Items.Insert(0, "---Select---"); }
In the above code, all the values in the second drop down list are getting duplicated based on the selected index changed event handler of the first drop down list. Can anyone help me with this? Been trying all kinds of stuff with this since three weeks now! H-e-L-p!
scarface
-
Here's the deal! I'm creating two drop down lists dynamically in a GridView control and populating the second dropdownlist(let's call it ddl2 from now on) on the basis of the selection on the first dropdownlist control. The crisis is that, for multiple rows on the gridview the ddl2 starts appending values to itself and therefore ends up having repeats of the data it should originally hold. How do I stop this repeat from happening? My code is as below.
<asp:GridView ID="grd" runat="server" OnRowDataBound="grd_RowDataBound"> <Columns> <asp:TemplateField HeaderText="names"> <ItemTemplate> <asp:DropDownList ID="name" runat="server" OnSelectedIndexChanged="onSelectChangeddl"></asp:DropDownList> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="age"> <ItemTemplate> <asp:DropDownList ID="age" runat="server" AutoPostBack="true"> </asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView>
In c# code, i am trying to change the values of the second drop down list based on the value selected in the first. I am doing this as below:
foreach (GridViewRow row in grd.Rows) { DropDownList ddl1 = (DropDownList)row.FindControl("name"); DropDownList ddl = (DropDownList)row.FindControl("age"); if (ddl.SelectedValue.ToString() != "---Select---") { } else { ddl.Items.Clear(); } SqlCommand SPComm = new SqlCommand("someStoredProcedure", myConn); SPComm.CommandType = CommandType.StoredProcedure; SqlParameter param = SPComm.Parameters.Add("@something", SqlDbType.NVarChar, 50); param.Value = something; dr = SPComm.ExecuteReader(); //dr is sqlDataReader int i = 0; while (dr.Read()) { ddl.Items.Insert(i, dr.GetString(0)); } dr.Close(); ddl.Items.Insert(0, "---Select---"); }
In the above code, all the values in the second drop down list are getting duplicated based on the selected index changed event handler of the first drop down list. Can anyone help me with this? Been trying all kinds of stuff with this since three weeks now! H-e-L-p!
scarface
Hi, I am also seeing the you are facing same kind of problem sice last couple of week. Please send me your complete requirement. at anil.bly@gmail.com I will develop complete solution for you and send it to u.
Regards Anil Pal