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

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 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.

Method 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.

Method 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 🙂

Method 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";
            }
        }
    }
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x