2011-11-30 52 views
2

我有一个下拉列表:的FindControl - 找不到下拉列表

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

一个不错的小家伙。我有一些代码找到它:

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

不..这只是我的myControl1不被设置......所以当我后来在我的代码尝试设置为true可见,这是行不通的。

任何想法?

+1

为什么你使用Page.Findcontrol访问下拉?为什么不使用“ddlGoalKeeper”来设置可见的 –

+1

使用'ddlGoalKeeper.Visible'时的错误? – V4Vendetta

+0

这很可能是Repeater的一部分,或者类似的东西。在这种情况下,你不会用这样的逻辑找到它,你必须使用ItemDataBound事件并处理那里的控件。 –

回答

4

我已经运行的一个原因是,如果控件是当网站使用母版页时不起作用。

你可以使用这个想法首先得到一个参考的母版页,然后得到的内容页面的权限控制:

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

为什么不设置ddlGoalKeeper.Visible = true;直?

+0

这个问题或答案? – sasjaq

0

我不确定你为什么试图使用FindControl,如果你想切换可见性,最简单的方法是使用ddlGoalKeeper.Visible,因为控件可用。

如果包含在其他控件中的某个网格包含一个网格,那么您必须在网格控件中找到它,就像网格中的特定行[gridrow1].FindControl("ddlGoalKeeper")那样它会更有意义。