Drop down list event handler in gridview

Mar 24 2009 12:42 PM

Hi all,

      I am creating two drop down lists inside a gridview using the itemTemplate. ie

<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?