gpt4 book ai didi

c# - 如何在没有 RowDataBound 的情况下在 GridView ItemTemplate 中找到 DropDownList?

转载 作者:行者123 更新时间:2023-11-30 15:38:57 25 4
gpt4 key购买 nike

我在 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");

最佳答案

您需要为每一行绑定(bind)下拉列表。尝试这样的事情

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

关于c# - 如何在没有 RowDataBound 的情况下在 GridView ItemTemplate 中找到 DropDownList?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10954834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com