Change a nested master page’s master dynamically

asp.netmaster-pages

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?

Best Answer

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.

Related Topic