2012-03-26 69 views
3

无法找到控件来查找某些内容,但在控件确实存在时返回null。使用FindControl无法找到控件

它被调用asp:button click(并且没有字段显示动态显示)。这些控件是标签,并具有runat服务器e.t.c.我使用Umbraco,正在考虑去Page.FindControl可能无法正常工作/因为它使用母版页?下面的代码:

<asp:Label ID="Reg_Name_Error" CssClass="error" runat="server" /> 

if (Page.FindControl("Reg_Name_Error") != null) { } 
+0

在哪里标签,在母版? – 2012-03-26 04:20:25

+0

你好,无法找到页面上的任何控件:( – Baconbeastnz 2012-03-26 04:21:14

+0

控件是在一个conteplaceholder我需要搜索吗? – Baconbeastnz 2012-03-26 04:21:36

回答

3

当您使用母版页,你必须写下面的代码

ContentPlaceHolder content; 
content = (ContentPlaceHolder)Master.FindControl("Your Content Place Holder Id"); 

然后写下面的代码

if (content != null) 
     { 
      if (content.FindControl("Reg_Name_Error") != null) 
+1

嘿,我已经尝试过,但它仍然返回null! – Baconbeastnz 2012-03-26 04:50:20

+0

你必须给你的内容在您的母版页中的占位符ID。 – 2012-03-26 05:00:37

+1

yip done that .. – Baconbeastnz 2012-03-26 05:02:43

相关问题