views:

159

answers:

3

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?

+7  A: 

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"; }
}
Sam Salisbury
+2  A: 

You can set the master page programmatically, however you can only do it in the pre-init event.

http://odetocode.com/articles/450.aspx

NickLarsen
But how can be a button related to this?
JMSA
+2  A: 

You can have a regular, non-server <form>, with a hidden <input> field. When the form posts, you check for the <input> value in the Pre_Init event, and change the Master Page there.

You can't use a server-side form with a regular button event, because they fire too late in the page life cycle.

RickNZ