473,382 Members | 1,692 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,382 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 16255
"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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.