【问题标题】:Accessing a Master Page using custom base class causes Page not to render使用自定义基类访问母版页会导致页面不呈现
【发布时间】:2012-07-10 00:59:31
【问题描述】:

我在使用 ASP.NET/C# 4.0 时遇到了一个非常奇怪的问题,我无法找到答案。我的子母版页有一个自定义基础母版页,我的页面有一个自定义基础页类。我的问题是,如果我从我的任何页面访问主成员,页面内容本身不会被呈现,只有母版页。

所以,以不同的方式回顾一下: “母版页”继承 System.Web.UI.MasterPage 使用“母版页”的“嵌套母版”继承 System.Web.UI.MasterPage

“WebForm”使用“Nested Master”,WebForm继承CustomPageBase,继承System.Web.UI.Page

当 CustomBaseMaster 访问 this.Master 时,它不会渲染 webform 的内容,只会渲染“嵌套母版”和“母版页”内容。我试过只读成员,写成员,读写同时,甚至不访问成员,只调用Debug.WriteLine(this.Master)

如果有帮助,我正在“WebForm”上使用页面装饰器,需要更新“母版页”上的控件

不幸的是,我的谷歌搜索查询显示了如何访问母版页的公共成员的结果。但是我找不到任何东西来解释为什么在访问母版页时会导致页面内容不显示。

有没有人看到类似的东西并可以提供任何建议?谷歌没有太多帮助,现在已经搜索了几个小时。

【问题讨论】:

    标签: asp.net c#-4.0 master-pages


    【解决方案1】:

    好的,当在类的顶部使用页面装饰器时,它会在 PreInit 完成之前分配页面类的属性。我使用 ILSpy 来查看访问页面的 Master 属性时会发生什么,如果在创建 Master 页面之前访问它(内部为 null)并且在调用所有 PreInit 方法之前,它将创建一个新的 master 页面并使用该内容。有点烦人。无论如何,我的解决方案是不要让属性在获取和设置它们时将值冒泡到父母版页,而是在我的基页和基母版中重载 OnLoad 事件,并在那里设置父级的属性。

    所以,不要这样做:

    namespace MyWebsite
    {
        public abstract class MyPageBase : Page
        {
            public int PropertyName
            {
                get
                {
                    return ((MyMaster)this.Master).RatePageId;
                }
                set
                {
                    ((MyMaster)this.Master).RatePageId;
                }
            }
        }
    }
    

    我这样做了:

    namespace MyWebsite
    {
        public abstract class MyPageBase : Page
        {
            public int? PropertyName
            {
                get;
                set;
            }
    
            protected override void OnInit(EventArgs e)
            {
                base.OnInit(e);
    
                if (PropertyName != null)
                {
                    ((MyMaster)this.Master).PropertyName = PropertyName.Value;
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-04
      • 1970-01-01
      相关资源
      最近更新 更多