Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Basics
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 29th, 2009, 04:32 AM
Registered User
 
Join Date: Dec 2008
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Default How to dynamically change Master pages?

I'm trying to add this into an application I've already written, and am stuck.

I have two master pages: default.master and alternate.master. I would like to allow a user to switch back-and-forth between the two master pages as often as they want - the master pages yield two very different designs.

I have a BasePage class.

I add the links/linkbuttons to the master pages from a *.ascx file with it's own codefile.

Somewhere in all of this, I can't figure out where I handle changing the master pages.

In the .ascx file, I am using LinkButtons (for the moment):

Code:
<asp:Linkbutton id="btnDefault" commandArgument="default" onCommand="Change_Master" causesValidation="false" text="DEFAULT SKIN" runat='server'/>
and:

Code:
<asp:Linkbutton id="btnAlternate" commandArgument="alternate" onCommand="Change_Master" causesValidation="false" text="ALTERNATE SKIN" runat='server'/>
In the codefule for the .ascx, I am using this:

Code:
 
protected void Change_Master (object sender, CommandEventArgs e)
{
Session["Page_Master"] = e.CommandArgument.ToString();
this.Response.Redirect(this.Request.Url.ToString());
}
In the individual page classes, I can detect and display the content of the Session["Page_Master"] object in the OnLoad event, but cannot detect it in the OnPreInit event. In the OnPreInit event, if I try to apply the session object to change the page master, I get a 404 error for a page dependency not found.

I don't know if the BasePage class has a role to play, what it should be doing in the BasePage_PreInit event, or if I handle it all within the individual page classes...

I have tinkered with this for so long I have completely confused myself, and would appreciate any help or guidance in how to untangle this Gordian knot.

Thanks, and Happy New Year!

- Tinker

Last edited by T. R. Tinker; December 29th, 2009 at 05:01 AM.. Reason: Added [code] [/code] tags.
 
Old December 29th, 2009, 02:56 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

PreInit is indeed the place. Here's some sample code (VB) from a BasePage class in an app where it works:

Code:
 Protected Overrides Sub OnPreInit(ByVal e As EventArgs)
  Dim myHostHeader As String = Me.HostHeader.ToLower().Replace(".test", "")
    Select Case myHostHeader
      Case "SomeSite"
        Me.MasterPageFile = Me.MasterPageFile.Replace("/MasterPages/", "/MasterPages/SomeMasterPage/")
      Case "SomeOtherSite"
        Me.MasterPageFile = Me.MasterPageFile.Replace("/MasterPages/", "/MasterPages/SomeOtherMasterPage/")
    End Select
    MyBase.OnPreInit(e)
End Sub
Not sure what you mean with "I get a 404 error for a page dependency not found." though...

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
The Following User Says Thank You to Imar For This Useful Post:
T. R. Tinker (December 30th, 2009)
 
Old December 29th, 2009, 06:15 PM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

Quote:
Originally Posted by Imar View Post
Not sure what you mean with "I get a 404 error for a page dependency not found." though...
I would guess that the path to one or both of the master pages is incorrect. If you're adapting Imar's code sample, you'll be defining the path differently which "should" take care of the problem. I wouldn't worry about it for now. The alternative is to confirm if the master pages are really in the correct path location.
__________________
-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.
The Following User Says Thank You to chroniclemaster1 For This Useful Post:
T. R. Tinker (December 30th, 2009)
 
Old December 30th, 2009, 04:44 AM
Registered User
 
Join Date: Dec 2008
Posts: 6
Thanks: 3
Thanked 0 Times in 0 Posts
Default

Thank you, Imar.

With very minor changes to adapt to my site's folder/directory structure, this is now working.

I first saw the "404" error when I started using .Net 3.5 and IE8, and the code for a dynamically-created control failed. I looked at the URL for the longest time until I was certain it was correct before I found the bug in my code. So it seems that if the page is incomplete, .Net 3.5 and IE8 will toss that kind of cryptic 404 error with the added sentence that a "page dependency might not have loaded or has been moved."

Thanks.
 
Old December 30th, 2009, 07:01 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

BTW, this line:

Dim myHostHeader As String = Me.HostHeader.ToLower().Replace(".test", "")
could be rewritten as

Dim myHostHeader As String = Me.HostHeader.ToLower()

or

Dim myHostHeader As String = Request.ServerVariables("HTTP_HOST")

The HostHeader property is local to the BasePage class and returns Request.ServerVariables("HTTP_HOST") with the port number and colon stripped off the path.

We used fake .test domain extensions in the environment so we needed to strip that off to make it a real domain.

Clearly, how you determine which Master Page to load is entirely up to you. I used the current host header, but you can use whatever you see fit (sessions, cookiies etc).

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Master Pages micptpuser ASP.NET 2.0 Basics 6 January 8th, 2009 04:50 AM
Master Pages Jayahar ASP.NET 3.5 Basics 1 September 2nd, 2008 04:20 PM
Master Pages, Content Pages and CSS carliviris Visual Studio 2005 0 January 8th, 2008 05:56 PM
Master pages wunnavabk ASP.NET 2.0 Professional 1 June 25th, 2007 07:09 AM
Dynamically changing Master Pages (Forms Auth) lancer ASP.NET 2.0 Basics 3 July 16th, 2006 10:59 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.