Re: How to dynamically change master page?

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"ad" <flying@xxxxxxxxxxxxxxx> wrote in message
news:e6nfih8kGHA.4776@xxxxxxxxxxxxxxxxxxxxxxx

For just one page, and change it for a while.

OK, then.

The *only* place that a content page can set its MasterPage is in its
Page_PreInit method, so you're going to have to do the following:

For each content page for which you want to enable dynamic MasterPages,
you'll need to set up a Session variable, which you'll need to initialise on
the Session_Start method in global.asax.

1) E.g. let's say you have a page called MyPage.aspx - in Session_Start,
you'll need to do the following:

Session["MyPage_Master"] = "~/master/Master1.master";

2) In the Page_PreInit of MyPage.aspx, you'll need to do the following:

private void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = Session["MyPage_Master"].ToString();
}

3) To change the MasterPage, you'll simply need to create an <asp:Button> or
any other webcontrol which can cause a postback, set the
Session["MyPage_Master"] session variable to the new MasterPage, then
Response.Redirect the page back to itself.

Thereafter, whenever MyPage.aspx is called, it will set its MasterPage to
whatever value is in the Session["MyPage_Master"] session variable.

Sounds a bit convoluted, but it's fairly straightforward once you get your
head round it... :-) Good luck!


.


Quantcast