Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET dynamic changing of master page

Is it possible to change the master-page of a content-page with the click of a button on that content-page?

If not why?

like image 699
user366312 Avatar asked Dec 24 '09 13:12

user366312


People also ask

Can master pages be nested?

Master pages can be nested, with one master page referencing another as its master. Nested master pages allow you to create componentized master pages. For example, a large site might contain an overall master page that defines the look of the site.


1 Answers

It is possible, you'll have to override the OnPreInit method of your codebehind class like so...

protected override void OnPreInit(EventArgs e)
{
    Page.MasterPageFile = "~/your/masterpage.master";
}

So to bind this to a click, you could use a query string parameter, i.e.

<a href="<%=Request.Url.ToString()%>?masterPage=alternative">Use
alternative master page</a>

And then in the codebehind

protected override void OnPreInit(EventArgs e)
{
    if(Request["masterPage"] == "alternative")
    { Page.MasterPageFile = "~/your/alternative/masterpage.master"; }
}
like image 104
Sam Salisbury Avatar answered Oct 29 '22 16:10

Sam Salisbury