Changing Master Pages at runtime

Last post 08-03-2007 1:59 AM by Amanda Wang - MSFT. 6 replies.

Sort Posts:

  • Changing Master Pages at runtime

    07-31-2007, 6:51 AM

    Hi,

    Is it possible to change the Master Page in the Postback.  I know you have to do it in the "OnPreInit" event but can you do it in a Postback.  The reason why I am asking this is because, in my site I am changing the Master Page at runtime in the "OnPreInit" even in a Postback and this causes a crash with the following error message.

    Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.

    Is there something I am missing here.

    Thanks
    Arjuna.

  • Re: Changing Master Pages at runtime

    07-31-2007, 7:18 AM
    • Contributor
      3,557 point Contributor
    • ravipahuja1
    • Member since 06-27-2007, 6:58 AM
    • Delhi NCR
    • Posts 629

     http://www.exforsys.com/tutorials/asp.net-2.0/changing-master-pages-dynamically-in-asp.net-2.0.html

  • Re: Changing Master Pages at runtime

    07-31-2007, 7:37 AM
    • Star
      8,834 point Star
    • MorningZ
    • Member since 07-22-2002, 2:39 PM
    • Fort Lauderdale, FL
    • Posts 1,815

    "Is there something I am missing here"

     Yes, that the only place you can change it is in the PreInit event, anything else is "too late"

    You'll have to get the user to refresh the page someway after selecting the new MasterPage choice, or have a response redirect after you save the setting

    "If you make it idiot proof, they'll build a better idiot"
  • Re: Changing Master Pages at runtime

    07-31-2007, 11:53 AM
    • Participant
      1,134 point Participant
    • mihirpathak
    • Member since 10-18-2002, 4:16 AM
    • Posts 199

    Hi, 

    The MasterPage class derives from UserControl. The master page will inject itself as the top control in a page’s control hierarchy by clearing the page’s Controls array and adding itself to into the collection. Doing so includes all of the markup and controls defined inside the master page in the page's control tree. The master page can then walk the Content controls that existed in the web form and bring them back them into the control hierarchy in the appropriate locations. The master page injection happens after the PreInit event fires for a Page object, but before the Init event fires.

    This ordering of events outlined above is important if you want to programmatically set the master page to use for a web form with the MasterPageFile property. This property must be set in the PreInit event (or earlier), like the code below. One the master page has injected itself into the control hierarchy it is too late to try to set a new master page for the web form. If you try to set the MasterPageFile property after the PreInit event fires, the runtime will throw an InvalidOperationException.

    Protected Sub Page_PreInit(ByVal sender As Object, _

                               ByVal e As EventArgs) _

                               Handles Me.PreInit

        ' we can select a different master page in the PreInit event

        Me.MasterPageFile = "~/otc.master"

     

    End Sub

     

     

    MP

    I never desire to converse with a man who has written more than he has
    read. -Samuel Johnson, lexicographer (1709-1784)
  • Re: Changing Master Pages at runtime

    07-31-2007, 1:46 PM
    • All-Star
      16,530 point All-Star
    • Dave Sussman
    • Member since 06-17-2002, 11:53 AM
    • UK
    • Posts 2,289
    • ASPInsiders
      TrustedFriends-MVPs

    The problem is, of course, that this only works on a page by page basis, so you'd have to add the PreInit event to every page. A better solution is to hook into a global event in Global.asax, allowing the master to be set dynamically for every page. Here's what I do:

        void Application_PreRequestHandlerExecute(object src, EventArgs e)
        {
            // hook up the PreInit page handler
            Page p = this.Context.Handler as Page;
            if (p != null)
            {
                p.PreInit += new EventHandler(page_PreInit);
            }
        }
        void page_PreInit(object sender, EventArgs e)
        {
            Page p = this.Context.Handler as Page;
            if (p != null)
            {
                // set the theme and master page
                p.Theme = "MyTheme";
                p.MasterPageFile = "~/MasterPages/MyMaster.master";
            }
        } 

    PreRequestHandlerExecute happens early enough in the lifecycle to set the master.

    d

     

     

  • Re: Changing Master Pages at runtime

    08-01-2007, 12:05 AM

    Hi,

     Thank you everyone for your replies, but this does not answer my question.  I am am changing the MasterPage in the PreInit event but I still get the ViewState error that I mentioned initially.  When I check IsPostback this returns true.

     Thanks

    Arjuna.

     

  • Re: Changing Master Pages at runtime

    08-03-2007, 1:59 AM
    Answer

    Hi Arjuna,

    You can read this article: http://weblogs.asp.net/guys/archive/2004/12/05/275321.aspx 

    Maybe, it can give you somehelp to solve your problem.

    Please let us know if it works.

     

    Amanda Wang
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (7 items)