【问题标题】:Can't find control using FindControl使用 FindControl 找不到控件
【发布时间】:2012-03-26 04:14:32
【问题描述】:

在查找控件时遇到问题,但当控件存在时它返回 null。

asp:button click 上调用它(并且没有字段显示动态)。控件是标签,并具有 runat server e.t.c.我正在使用 Umbraco,正在考虑 Page.FindControl 可能无法工作 /w umbraco 因为它使用母版页?代码如下:

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

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

【问题讨论】:

  • 标签在哪里,在 MasterPage 中?
  • 是的,在页面上找不到任何控件:(
  • 控件在一个conteplaceholder中,我需要在其中搜索吗?
  • 抱歉不是ontentplaceholder,而是
  • 也添加相关标记..

标签: asp.net


【解决方案1】:

当您使用母版页时,您必须编写以下代码

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

然后编写如下代码

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

【讨论】:

  • 您必须提供母版页中的内容占位符 ID。
猜你喜欢
  • 2012-08-06
  • 1970-01-01
  • 1970-01-01
  • 2014-05-06
  • 1970-01-01
  • 2010-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多