Drop Down Lists
-
Hi friends, I want to create drop down lists for all the rows of a gridview. But i dont want to use TemplateField because i want to give different id's for all drop downs. Can anybody help me please.
scarface
-
Hi friend, Can u provide yr exact requirement, like why u need to give unique ID to all Dropdown list at server side. So that i can help u. And what problem u have with TemplateField?
Regards Anil Pal
Hi, I am trying to create two sets of drop down lists in a gridview. If i change the index of the first drop down, the values of the corresponding drop down should change. I used event handler for the first drop down, but if i change the index of any one of the drop downs in the first set, all the values of second drop downs are getting changed.
scarface
-
Hi, I am trying to create two sets of drop down lists in a gridview. If i change the index of the first drop down, the values of the corresponding drop down should change. I used event handler for the first drop down, but if i change the index of any one of the drop downs in the first set, all the values of second drop downs are getting changed.
scarface
Hi, Now i understand yr problem. So the initialization of second set of dropdown list u can not stop, while changing of index of first set of drop down list. So to maintain the state of the second dropdown set. First u need to take the state of all the dropdown list from second set and the again u need to set their state while changing the index of first one. Now please let me know if you code for that or any query.
Regards Anil Pal
-
Hi, I am trying to create two sets of drop down lists in a gridview. If i change the index of the first drop down, the values of the corresponding drop down should change. I used event handler for the first drop down, but if i change the index of any one of the drop downs in the first set, all the values of second drop downs are getting changed.
scarface
Please have a look at my code:
<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.
scarface
-
Please have a look at my code:
<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.
scarface
Hi, I have gone through the your code. int i = 0; while (dr.Read()) { ddl.Items.Insert(i, dr.GetString(0)); } dr.Close(); In the above code where u increment the I . According to yr code there always i=0. Hence it replicate the value. Why u can not use Datatable or dataset. Howevere using of Collection of object is good choice.
Regards Anil Pal