【发布时间】:2012-05-05 21:20:57
【问题描述】:
您好,我已经被这种情况困扰了很长时间了?在这里寻求帮助?
我已经定义了嵌套母版页(WebApp 的 1 个默认值和其他引用默认值的自定义母版页)
以下是使用我的自定义母版页的内容页面(比如 content.aspx)的代码片段
在我包含的 aspx 源代码中:
%@ MasterType VirtualPath="~/NestedMasterPage1.master" %>
在文件后面的代码中,即。 custom.aspx.cs(这就是问题所在):
ContentPlaceHolder masterContentPlaceHolder = (ContentPlaceHolder)Page.Master.Master.FindControl("MainContent"); //works well
ContentPlaceHolder nestedContentPlaceHolderHeading = (ContentPlaceHolder)masterContentPlaceHolder.FindControl("NestedMasterHeading"); //works well
Label NewsHeadLines = (Label)nestedContentPlaceHolderHeading.FindControl("lblSubSectionHeader"); //returns null?? The Control ID is all checked and is the same in the Nested Master Page.
**NewsHeadLines.Text = "Testing";** //System.NullReferenceException:
对象引用未设置为对象的实例。
这是因为我在 HTML 表格、行和列中嵌套了我的控件(这里是标签)吗?请指教?我已经仔细检查了控件名称或 ID。
【问题讨论】:
-
您应该在母版页中提供公共属性,而不是使用 FindControl,以提供对相应控件属性的访问(例如
Text)。在这种情况下,您可以实现属性NewsHeadLines。您始终可以在(母版)页面中直接引用他们的控件。因此,您只需将 master 转换为正确的类型,然后才能访问这些属性。 -
好主意蒂姆。虽然错过了那条线。谢谢。
标签: asp.net master-pages webforms