从代码隐藏中添加它们后,我试图在 div 中访问 DropDownList。如果我像这样将 DropDownList 添加到我的 .aspx 页面:
<div id="FiltersDiv" runat="server">
<asp:DropDownList ID="DropDownListMenu" runat="server"
DataTextField="name" DataValueField="id"
AutoPostBack="true"
onselectedindexchanged="DropDownListMenu_SelectedIndexChanged" >
</asp:DropDownList>
我可以在任何情况下访问
protected void AddButton_Click(object sender, EventArgs e)
{
var dl = addProductLocationFiltersDiv.Controls.OfType<DropDownList>();
}
但如果我从代码中添加
var ddl = new DropDownList();
ddl.DataSource = flist;
ddl.DataTextField = "name";
ddl.DataValueField = "id";
ddl.DataBind();
FiltersDiv.Controls.Add(ddl);
我需要添加几个 DropDownList,我现在不知道会有多少。所以,我需要循环。我错过了一些属性还是我需要怎么做?