如何在没有RowDataBound的GridView ItemTemplate中找到DropDownList?

时间:2012-06-08 19:17:17

标签: c# asp.net

我在GridView之外有一个DropDownList,我在GridView的ItemTemplate中有一个DropDownList。外面的DropDownList有一个SelectedIndex_Changed事件,当它触发时,它应该填充GridView内的DropDownList。问题是在我用来填充内部DropDownList的方法中,它无法找到控件:这是在更改外部DropDownList时调用的示例代码:

 //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();
    }

我也尝试过:

DropDownList ddlRoom = (DropDownList)gv.FindControl("ddlRoom");

1 个答案:

答案 0 :(得分:4)

您需要绑定每一行的下拉列表。试试这样的事情

DropDownList ddlRoom = null;
foreach(var gridRow in gv.Rows)
{
    ddlRoom = gridRow.FindControl("ddlRoom") as DropDownList;
    if (ddlRoom != null)
    {
        //your code here
    }
}