FindControl - 找不到下拉列表

Oed*_*dum 3 c# asp.net findcontrol drop-down-menu

我有一个下拉列表:

<asp:DropDownList ID="ddlGoalKeeper" runat="server">
                </asp:DropDownList>
Run Code Online (Sandbox Code Playgroud)

一个不错的小人。我有一些代码可以找到它:

DropDownList myControl1 = (DropDownList)Page.FindControl("ddlGoalKeeper");
Run Code Online (Sandbox Code Playgroud)

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

有任何想法吗?

Jay*_*Jay 5

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

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

 ContentPlaceHolder MainContent = Page.Master.FindControl("MainContent") as ContentPlaceHolder;
 DropDownList myControl1 = (DropDownList)MainContent.FindControl("ddlGoalKeeper");
Run Code Online (Sandbox Code Playgroud)