无法使用FindControl找到控件

3

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

它被调用于一个asp:button click(没有任何字段显示动态)。这些控件是标签,并且具有运行服务器等。我正在使用Umbraco,考虑到使用Page.FindControl在Umbraco中可能无法工作,因为它使用主页面?以下是代码:

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

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

标签在哪里,在MasterPage中吗? - Adrian Iftode
哎呀,页面上找不到任何控件 :( - williamsandonz
控件在一个容器中,我需要在其中搜索吗? - williamsandonz
抱歉,这不是一个内容占位符,而是一个 <content>。 - williamsandonz
添加相关标记... - Adrian Iftode
1个回答

3
由于您正在使用主页面,因此您需要编写以下代码:

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

那么请写下以下代码。
if (content != null)
        {
            if (content.FindControl("Reg_Name_Error") != null)

你需要在主页面中给出你的内容占位符ID。 - Vishal Patel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接