Do Masterpages have a preinit event?

Last post 10-02-2006 2:24 PM by bitmask. 7 replies.

Sort Posts:

  • Do Masterpages have a preinit event?

    09-28-2006, 1:09 PM

    I have this code:

    1    public partial class MasterPage : System.Web.UI.MasterPage
    2    {
    3        protected void Page_Load(object sender, EventArgs e)
    4        {
    5            if (!IsPostBack)
    6            {
    7                Label1.Text = Profile.FullName;
    8            }
    9        }
    10       protected void Page_PreInit(object sender, EventArgs e)
    11       {
    12           Response.Write("HI");
    13           Page.Theme = Profile.MyTheme;
    14       }
    15   }
    

     

    If Masterpages do not have the preinit event, then how can I set the theme dynamically for all my pages that are based upon the single Masterpage?

    Thank you for any help. 

     

  • Re: Do Masterpages have a preinit event?

    09-28-2006, 9:56 PM
    Answer
    • Contributor
      3,041 point Contributor
    • lostlander
    • Member since 05-22-2006, 11:55 AM
    • Posts 607

    Masterpage doesn't have PreInit method.

    There are several alternatives you can adopt.

    1, Create a common base page class for all other pages to inherit, set the theme property in that class;
    http://www.odetocode.com/Articles/450.aspx

    2, Set the global theme in web.config:
    <configuration>
        <system.web>
            <pages theme="ThemeName" />
        </system.web>
    </configuration>
    http://msdn2.microsoft.com/en-us/library/0yy5hxdk.aspx

    Regards.
    lostlander.

  • Re: Do Masterpages have a preinit event?

    09-29-2006, 2:33 AM
    • Member
      10 point Member
    • PaxGalactica
    • Member since 09-29-2006, 6:16 AM
    • Posts 2

    I need to do this very thing as well: using master pages with dynamically chosen themes. And it can be done. While the master page object may not have the Page_PreInit() method, the content page object does! So, if I had two files "Pgo.master" and "Default.aspx" with the aspx page using the master page, the "Default.aspx.cs" file (I like using code-behind) could have code something like this:

    (C# code)
    public partial class _Default : System.Web.UI.Page
    {
       protected void Page_Load(object sender, EventArgs e)
       {
          // Just to show the page title being changed too.
          Page.Header.Title = "Pax Galactica Online";
       }

       protected void Page_PreInit(object sender, EventArgs e)
       {
          // The page theme is dynamically updated here
          Page.Theme = Profile.MyTheme;
       }

    }

  • Re: Do Masterpages have a preinit event?

    09-29-2006, 2:51 AM
    • Member
      10 point Member
    • PaxGalactica
    • Member since 09-29-2006, 6:16 AM
    • Posts 2

    I almost forgot to mention this. The original poster asked

        "If Masterpages do not have the preinit event, then how can I set the theme dynamically for all my
         pages that are based upon the single Masterpage?"

    The question points out something which can be misleading. Master pages are not objects from which pages that use them are derived. Rather, think of master pages as formatting files. The content pages are the top-level objects, with the "master" page being just an example of how you want the web server to render the common elements among your pages.

    An important result of this then is that all code that is not common to all pages should be placed in the content pages, not in the master page(s). The content pages are actual Page objects, so they have all the methods and proprties of "stand-alone" Page objects (those not using master pages).

    Some New Guy
    http://www.paxgalacticaonline.com/

  • Re: Do Masterpages have a preinit event?

    10-02-2006, 1:23 PM
    PaxGalactica:

    I need to do this very thing as well: using master pages with dynamically chosen themes. And it can be done. While the master page object may not have the Page_PreInit() method, the content page object does! So, if I had two files "Pgo.master" and "Default.aspx" with the aspx page using the master page, the "Default.aspx.cs" file (I like using code-behind) could have code something like this:

    (C# code)
    public partial class _Default : System.Web.UI.Page
    {
       protected void Page_Load(object sender, EventArgs e)
       {
          // Just to show the page title being changed too.
          Page.Header.Title = "Pax Galactica Online";
       }

       protected void Page_PreInit(object sender, EventArgs e)
       {
          // The page theme is dynamically updated here
          Page.Theme = Profile.MyTheme;
       }

    }

     

    This means I need it in every single .aspx page.  Correct?  What if it is an existing site with hundreds of pages?

  • Re: Do Masterpages have a preinit event?

    10-02-2006, 1:23 PM
    PaxGalactica:

    I need to do this very thing as well: using master pages with dynamically chosen themes. And it can be done. While the master page object may not have the Page_PreInit() method, the content page object does! So, if I had two files "Pgo.master" and "Default.aspx" with the aspx page using the master page, the "Default.aspx.cs" file (I like using code-behind) could have code something like this:

    (C# code)
    public partial class _Default : System.Web.UI.Page
    {
       protected void Page_Load(object sender, EventArgs e)
       {
          // Just to show the page title being changed too.
          Page.Header.Title = "Pax Galactica Online";
       }

       protected void Page_PreInit(object sender, EventArgs e)
       {
          // The page theme is dynamically updated here
          Page.Theme = Profile.MyTheme;
       }

    }

     

    This means I need it in every single .aspx page.  Correct?  What if it is an existing site with hundreds of pages?

     
  • Re: Do Masterpages have a preinit event?

    10-02-2006, 1:24 PM
    PaxGalactica:

    I need to do this very thing as well: using master pages with dynamically chosen themes. And it can be done. While the master page object may not have the Page_PreInit() method, the content page object does! So, if I had two files "Pgo.master" and "Default.aspx" with the aspx page using the master page, the "Default.aspx.cs" file (I like using code-behind) could have code something like this:

    (C# code)
    public partial class _Default : System.Web.UI.Page
    {
       protected void Page_Load(object sender, EventArgs e)
       {
          // Just to show the page title being changed too.
          Page.Header.Title = "Pax Galactica Online";
       }

       protected void Page_PreInit(object sender, EventArgs e)
       {
          // The page theme is dynamically updated here
          Page.Theme = Profile.MyTheme;
       }

    }

     

    This means I need it in every single .aspx page.  Correct?  What if it is an existing site with hundreds of pages?

     Thanks.
  • Re: Do Masterpages have a preinit event?

    10-02-2006, 2:24 PM
    Answer
    • Contributor
      6,537 point Contributor
    • bitmask
    • Member since 07-29-2003, 11:18 AM
    • Citizen of the Earth
    • Posts 1,228
    just_for_forums:

    This means I need it in every single .aspx page.  Correct?  What if it is an existing site with hundreds of pages?

    An alternative is to use an HttpModule and hook the PreInit event of a page.

    See my articles: Taking Care of PreInit and Master Pages: Tips, Tricks, Traps.

    Scott
    http://www.OdeToCode.com/blogs/scott/
Page 1 of 1 (8 items)