How to dynamically change master page?

M

Mark Rae

How can I dynamically change the MasterPager of a web page?

private void Page_PreInit(object sender, EventArgs e)
{
this.MasterPageFile = "MyNewPasterPage.master" // amend as
appropriate
}
 
A

ad

Thanks,
But if I wish when an user press a button on web page, it will do the change
of mastPage.
How can I do that?
 
M

Mark Rae

Thanks,
But if I wish when an user press a button on web page, it will do the
change of mastPage.
How can I do that?

Do you want the selected MasterPage to be for the whole site (for the
current user), or just for the current page?
 
A

ad

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


Mark Rae said:
Do you want the selected MasterPage to be for the whole site (for the
current user), or just for the current page?
 
M

Mark Rae

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!
 
A

ad

Thanks,
Your answer is great, but I have a question about your answer.
Why we need to Response.Redirect the page back to itself after change the
Session variable of MasgerPage.
I have test if it didn't Response.Redirect the page back to itself, just
let the page postback, the MasterPage will not change at the fist postback,
I must postbak twice to make the change.

Why?




Mark Rae said:
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!
 
M

Mark Rae

Your answer is great, but I have a question about your answer.
Why we need to Response.Redirect the page back to itself after change the
Session variable of MasgerPage.
I have test if it didn't Response.Redirect the page back to itself, just
let the page postback, the MasterPage will not change at the fist
postback, I must postbak twice to make the change.

Why?

Because the MasterPage can *only* be set in a content page's Page_PreInit
method.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,294
Latest member
LandonPigo

Latest Threads

Top