从母版页访问ControlsPlace持有者

时间:2012-05-05 21:20:57

标签: asp.net master-pages webforms

您好我一直坚持这种情况已经有一段时间了吗?在这里寻求帮助?

我已经定义了嵌套母版页(1个默认用于WebApp和其他自定义母版页引用默认版本)

以下是使用我的自定义母版页的内容页面(例如content.aspx)中的代码snipet

在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表格,行和列中嵌套了我的Control(此处为标签)?请指教?我仔细检查了控件名称或ID。

1 个答案:

答案 0 :(得分:0)

我相信您可能缺少一个或两个嵌套容器。试试这个;而不是你试图弄清楚嵌套容器尝试有一个共同的编程逻辑来获取它们。看看Rick Strahl的blog post