简体   繁体   English

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

[英]How to find a DropDownList in a GridView ItemTemplate without RowDataBound?

I have a DropDownList outside of a GridView and I have a DropDownList inside an ItemTemplate of a GridView. 我在GridView之外有一个DropDownList,我在GridView的ItemTemplate中有一个DropDownList。 The DropDownList that is outside has a SelectedIndex_Changed event and when that fires, it should populate the DropDownList inside the GridView. 外面的DropDownList有一个SelectedIndex_Changed事件,当它触发时,它应该填充GridView内的DropDownList。 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: 问题是在我用来填充内部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();
    }

I have also tried: 我也尝试过:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM