How to find a DropDownList in a GridView ItemTemplate without RowDataBound?
asp.net, c#
Solution
You'll need to bound the dropdown for each row. Try something like this
DropDownList ddlRoom = null;
foreach(var gridRow in gv.Rows)
{
ddlRoom = gridRow.FindControl("ddlRoom") as DropDownList;
if (ddlRoom != null)
{
//your code here
}
}
Problem
I have a DropDownList outside of a GridView and I have a DropDownList inside an ItemTemplate of a GridView. The DropDownList that is outside has a SelectedIndex_Changed event and when that fires, it should populate the DropDownList inside the GridView. The problem is that in the method that I use to populate the inside DropDownList, it can't find the control: Here is sample code that is called when the outside DropDownList is changed: ``` //Does not find ddlRoom DropDownList ddlRoom = (DropDownList)gv.TemplateControl.FindControl("ddlRoom"); if (rows.Count() > 0) { var rooms = rows.CopyToDataTable(); ddlRoom.Items.Clear(); ddlRoom.Items.Add(new ListItem("Select...", "-1")); ddlRoom.DataSource = rooms; ddlRoom.DataBind(); } ``` I have also tried: ``` DropDownList ddlRoom = (DropDownList)gv.FindControl("ddlRoom"); ```