473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

How to dynamically change master page?

ad
Hi,
How can I dynamically change the MasterPager of a web page?
Jun 17 '06 #1
9 16256
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:O9****************@TK2MSFTNGP02.phx.gbl...
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
}
Jun 17 '06 #2
The MasterPage does not support the Page_PreInit so it has to be used in
each content page or a base class must be used which all content pages
inherit from.

<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://www.metromilwaukee.com/clintongallagher/

"Mark Rae" <ma**@markN-O-S-P-A-M.co.uk> wrote in message
news:OG**************@TK2MSFTNGP04.phx.gbl...
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:O9****************@TK2MSFTNGP02.phx.gbl...
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
}

Jun 18 '06 #3
"clintonG" <cs*********@REMOVETHISTEXTmetromilwaukee.com> wrote in message
news:O4**************@TK2MSFTNGP03.phx.gbl...
The MasterPage does not support the Page_PreInit
I didn't say it did...
so it has to be used in each content page or a base class must be used
which all content pages inherit from.


Correct.
Jun 18 '06 #4
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?
"Mark Rae" <ma**@markN-O-S-P-A-M.co.uk> ¼¶¼g©ó¶l¥ó·s»D:OG**************@TK2MSFTNGP04.phx.g bl...
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:O9****************@TK2MSFTNGP02.phx.gbl...
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
}

Jun 19 '06 #5
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:Oq**************@TK2MSFTNGP05.phx.gbl...
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?
Jun 19 '06 #6
ad
For just one page, and change it for a while.
"Mark Rae" <ma**@markN-O-S-P-A-M.co.uk> ¼¶¼g©ó¶l¥ó·s»D:%2****************@TK2MSFTNGP05.phx .gbl...
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:Oq**************@TK2MSFTNGP05.phx.gbl...
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?

Jun 19 '06 #7
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:e6**************@TK2MSFTNGP05.phx.gbl...
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!
Jun 19 '06 #8
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" <ma**@markN-O-S-P-A-M.co.uk> ¼¶¼g©ó¶l¥ó·s»D:uT****************@TK2MSFTNGP04.phx .gbl...
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:e6**************@TK2MSFTNGP05.phx.gbl...
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!

Jun 20 '06 #9
"ad" <fl****@wfes.tcc.edu.tw> wrote in message
news:uh**************@TK2MSFTNGP05.phx.gbl...
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.
Jun 20 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rob Roberts | last post by:
In a VS2005 ASP.NET project, I'm trying to find a way to change which css file is linked in based on the browser type. I'd like to use one css file for IE browsers, and a different one for all...
3
by: Ron | last post by:
Hi all, I've come across a problem when casting user controls in ASP.NET 2.0, hopefully someone can help. I have a master page with a user control on it (myMenu) that I wish to programmatically...
2
by: Mark Rae | last post by:
Hi, It's easy enough to get a content page to refer to public properties on its associated master page e.g. public partial class master_secure : System.Web.UI.MasterPage { public Label...
4
by: David Lozzi | last post by:
Howdy, I have my master page, and I would like to change the background CSS class per the content page. Only the home page has a different background style, all other pages are using the same. I...
12
by: Mark Rae | last post by:
Hi, It's easy enough for a child page to manipulate its MasterPage's header simply by modifying the MasterPage thus: <header runat="server"> .... .... </header>
23
by: LvBohemian | last post by:
I am playing around with creating some menus dynamically, and they create fine and show up ok when I want them to; but when I select a menu NavigateUrl at run time, the applicable url shows up...
1
by: Dinu | last post by:
How to Dynamically program User controls in a Master page from a content page Thanks
4
by: CJM | last post by:
I have a site that is being built using master pages, and includes a menu that I want to manipulate dynamically. Specifically the currently-selected menu option is styled differently to the...
6
by: _Who | last post by:
I use the code below to change to a style sheet that has: body { ....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.