c# - FindControl - 找不到下拉列表

标签 c# asp.net drop-down-menu findcontrol

我有一个下拉列表:

<asp:DropDownList ID="ddlGoalKeeper" runat="server">
                </asp:DropDownList>

一个可爱的小家伙。我有一些代码可以找到它:

DropDownList myControl1 = (DropDownList)Page.FindControl("ddlGoalKeeper");

不是..只是我的 myControl1 没有设置...所以当我稍后在我的代码中尝试将 visible 设置为 true 时,它​​不起作用。

有什么想法吗?

最佳答案

我遇到过它不起作用的一个原因是如果控件是在网站使用母版页时。

您可以使用这个想法首先获取对母版页的引用,然后从内容页获取正确的控件:

 ContentPlaceHolder MainContent = Page.Master.FindControl("MainContent") as ContentPlaceHolder;
 DropDownList myControl1 = (DropDownList)MainContent.FindControl("ddlGoalKeeper");

关于c# - FindControl - 找不到下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8324994/

相关文章:

c# - 在 ASP.NET MVC 中调用任何 Controller 操作之前处理 cookie

c# - 如何创建更好看的 DataGridView?

c# - 在 Windows 7 中使用备用数据流(分支)?

css - 菜单图标不消失

css - 在桌面上隐藏一个div

c# - Crystal Reports 中的组过滤

c# - asp.net/c# 中的线程

asp.net - 连接字符串最佳实践

c# - 在新构建后保留 Session[] 变量

c# - 将选定的 dropDownList 文本声明为隐藏字段,以便可以与 JS 一起使用