Search code examples
asp.netmaster-pages

How to dynamically change master page's master page?


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.


Solution

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