如何动态更改母版页的母版页?

发布于 2024-09-24 15:56:06 字数 245 浏览 2 评论 0原文

我正在尝试动态更改母版页,尽管从内容页(覆盖 OnPreInit)很容易做到,但母版页没有此类事件。是否可以以某种方式介绍这个活动?

更新:我通过梯子底部页面的PreInit到达了一半,事实证明你可以做base.Master.MasterPageFile之类的事情= "/master.Master";,但由于某种原因,这不会加载最顶层母版页标题中的内容,即样式表。

I am trying to change the master page dynamically, and although it's easy to do from a content page (overriding OnPreInit), there is no such event for a master page. Is it possible to introduce this event somehow?

UPDATE: I got halfway there by going via the PreInit of the pages at the bottom of the ladder, turns out you can do things like base.Master.MasterPageFile = "/master.Master";, but for some reason this doesn't load the stuff in the header of the top-most master page, namely stylesheets.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒与心事 2024-10-01 15:56:06

引用自: 我可以动态更改嵌套母版页的母版吗?< /a>

刚刚对此进行了测试,它可以在使用嵌套母版页的页面的 PreInit 中工作。
protected void Page_PreInit(对象发送者, EventArgs e)
{
this.Master.MasterPageFile = "/Site2.Master";
}

您需要确保 ContentPlaceholderIds 在您交换的页面之间保持一致。

Quoting from: Can I change a nested master page's master dynamically?

Just tested this and it works from the PreInit of the Page that is using the nested MasterPage.
protected void Page_PreInit(object sender, EventArgs e)
{
this.Master.MasterPageFile = "/Site2.Master";
}

Obviously you will need to ensure that the ContentPlaceholderIds are consistent across the pages you are swapping between.

安穩 2024-10-01 15:56:06

如果您覆盖 MasterPageClass 并添加您自己的 onPreInit 您可能可以做到这一点,但我认为即使这样也行不通。根据 Reflector 的说法,它绝对没有构造,甚至没有什么可以覆盖的,尽管因为它继承了 UserControl 那么总是有 OnInit ...或者你可以尝试覆盖 get_Master() 但这可能也不起作用...

If you overrode the MasterPageClass and added your own onPreInit you might could do it, but I don't think even that would work. There's definitely no construct for it according to Reflector, nothing to even override, altho since it inherits UserControl then there's always OnInit ... alternately you could attempt to override get_Master() but that might not work either ...

菊凝晚露 2024-10-01 15:56:06

使用母版页构造函数。

Use the masterpage constructor.

铃予 2024-10-01 15:56:06

假设您想使用不带菜单的不同母版页,请传递查询字符串 NoMenu。

protected void Page_PreInit(object sender, EventArgs e)
 {
   //You'll go through infinite loop if you do not check if we already have the new master page, this will switch to different master page if requested without a menu for example
   if (Request.QueryString["NoMenu"] != null && this.MasterPageFile != "/MasterPageNoMenu.master")
    {
        this.MasterPageFile = "/MasterPageNoMenu.master";

        base.OnPreInit(e); 
    }
} 

Let's say you want to use a different master page without a menu, pass query string NoMenu.

protected void Page_PreInit(object sender, EventArgs e)
 {
   //You'll go through infinite loop if you do not check if we already have the new master page, this will switch to different master page if requested without a menu for example
   if (Request.QueryString["NoMenu"] != null && this.MasterPageFile != "/MasterPageNoMenu.master")
    {
        this.MasterPageFile = "/MasterPageNoMenu.master";

        base.OnPreInit(e); 
    }
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文