Can I change a nested master page's master dynamically?

你说的曾经没有我的故事 提交于 2019-12-17 19:28:16

问题


Okay, so we all know about changing a master page dynamically in a page's OnPreInit event.

But what about a nested master page? Can I change a master's master?

There is no OnPreInit event exposed in the MasterPage class.

Any ideas?


回答1:


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.




回答2:


We combine Andy's method with a "BasePage" class - we create a class that inherits from System.Web.UI.Page, and then all our pages inherit from this class.

Then, in our base page class, we can perform the relevant checks to see which root master page should be used - in our case we have a "Presentation" master, and an "Authoring" master - the presentation version has all the navigation and page furniture, along with heavy display CSS, while the authoring master has some extra JS for the authoring tools, lighter CSS, and no navigation (it's what we use when the user is actually authoring a page, rather than modifying the site layout).

This base page can then call Page.Master.MasterPageFile and set it to the Authoring master if that is the correct state for the page.




回答3:


Just in case anyone stumbles across this and tears their hair out with a "Content controls have to be top-level controls in a content page or a nested master page that references a master page" error when trying Andy's code, get rid of the this.Master. So, the code becomes:

protected void Page_PreInit(object sender, EventArgs e)
{
    MasterPageFile = "/Site2.Master";
}

Edit As Zhaph points out below, the code I have ^^ there will only change the current page's master, not the master's master. This is the code Hainesy was talking about when he mentioned "we all know about changing a master page dynamically" (which I didn't, d'oh). If you happen to get to this page by googling "stackoverflow change master page" (which is what I did) then this is possibly the code you're looking for :-)




回答4:


To add on to the answer of Zhaph - Ben Duguid, (+1 by the way):

Here is example code that sets the master page of the nested master page. All pages inherit from this BasePage, so this code only exists in one place.

public class BasePage : System.Web.UI.Page
{
    private void Page_PreInit(object sender, System.EventArgs e)
    {
        if (Request.Browser.IsMobileDevice)
        {
            if (Page.MasterPageFile == "~/master/nested.master")) 
            {
                Page.Master.MasterPageFile = "~/master/mobile.master";
            } 
            else 
            {
                MasterPageFile = "~/master/mobile.master";
            }
        }
    }
}


来源:https://stackoverflow.com/questions/581950/can-i-change-a-nested-master-pages-master-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!