Multiple DropDownLists Inside the GridView Item Template Field

Position Reset) >> Multiple DropDownLists Inside the GridView Item Template FieldFriday, September 15, 2006 2:02 PM
Sometimes you have two DropDownLists inside the GridView Item Tempalte Field. This scenario is encountered when the DropDownList have a parent-child relationship. So, you want to select some item from the parent and fill the child list based on the parent selection. If GridView control was not involved in this triangle then it would have been pretty simple but GridView makes it little different.

The first thing that you need to do is to put the AutoPostBack on the parent DropDownList to true. Now, you can implement the SelectedIndex_Changed event of the DropDownList (Parent).

protected void ddl1_SelectedIndexChanged(object sender, EventArgs e)

{

string foundID = String.Empty;

GridViewRow row = (GridViewRow)((sender) as Control).NamingContainer;

// get the value from the first dropdownlist

DropDownList ddlFirst = ((sender) as DropDownList);

int ddlFirstSelectedValue = Convert.ToInt32(ddlFirst.SelectedValue);

string firstDropDownListID = ddlFirst.UniqueID;

// and now here is how

DropDownList ddlSecond = row.FindControl("ddl2") as DropDownList;

// now from here you can do whatever you want!

}

Nothing too fancy about the above code. I just used the NamingContainer to find the selected row in which the user has changed the DropDownList (Parent) selection. Then I get the value from the parent DropDownList and populate the child DropDownList by simply referering it by its ID.

This is it! 

你可能感兴趣的:(template)