如何找到没有的RowDataBound在GridView ItemTemplate中一个DropDownList? [英] How to find a DropDownList in a GridView ItemTemplate without RowDataBound?
问题描述
我有一个DropDownList的GridView的外面,我有一个GridView的一个ItemTemplate里面一个DropDownList。这是外DropDownList中有一个SelectedIndex_Changed事件和火灾时,应填充GridView控件内的DropDownList。问题是,在我用填充内DropDownList的方法,它不能找到控制:下面是示例code,当外界的DropDownList改变被称为:
//不找ddlRoom
DropDownList的ddlRoom =(DropDownList的)gv.TemplateControl.FindControl(ddlRoom);
如果(rows.Count()大于0)
{ VAR房= rows.CopyToDataTable();
ddlRoom.Items.Clear();
ddlRoom.Items.Add(新的ListItem(选择..., - 1));
ddlRoom.DataSource =室;
ddlRoom.DataBind();
}
我也尝试:
DropDownList的ddlRoom =(DropDownList的)gv.FindControl(ddlRoom);
您需要绑定的下拉列表中的每一行。尝试这样的事情
DropDownList的ddlRoom = NULL;
的foreach(在gv.Rows VAR gridRow)
{
ddlRoom = gridRow.FindControl(ddlRoom)作为DropDownList的;
如果(ddlRoom!= NULL)
{
//你的code在这里
}
}
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");
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
}
}
这篇关于如何找到没有的RowDataBound在GridView ItemTemplate中一个DropDownList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!