views:

287

answers:

2

Hi


1) I assume Themes can be set programatically only inside Page.PreInit event handler due to the following reasons:

  • if we’d set a Theme inside Page.Init event handler, then by that time ViewState would already be tracked and thus any data applied by Theme would be tracked and marked as dirty ( which would consume lot of bandwidth ) ?

  • and if we’d set it after Init event, then Themes could also override deserialized ViewState data applied to individual controls?


Are there any other reasons why Themes can’t be set after Page.PreInit?


2) Also, why can't Master pages be applied after Page.PreInit?

thanx

+1  A: 

According to this:

http://odetocode.com/articles/450.aspx

The 'MasterPageFile' property can only be set in or before the 'Page_PreInit' event.

This exception makes sense, because we know the master page has to rearrange the page’s control hierarchy before the Init event fires

The article also includes this example:

using System;
using System.Web.UI;

public class BasePage : Page
{
   public BasePage()
   {
        this.PreInit += new EventHandler(BasePage_PreInit);
   }

    void BasePage_PreInit(object sender, EventArgs e)
    {
        MasterPageFile = "~/Master1.master";
    }
}

Or, an approach I've used before:

protected override void OnPreInit(EventArgs e) 
    { 
        base.OnPreInit(e); 
        if (Request.QueryString["Master"] == "Simple") 
            MasterPageFile = "~/Masterpages/Simple.Master"; 
    }
davek
Is my assumption about why the Themes can't be set after PreInit correct?
carewithl
I'm not sure about themes, but for master pages the control hierarchy would be decisive.
davek
thanx for helping me out
carewithl
+1  A: 

Are there any other reasons why Themes can’t be set after Page.PreInit?

Yes. Themes includes skins, which can specify properties for controls. Those properties need to be set during the Init event, so the desired theme needs to be selected before then.

ViewState tracking may be an issue, but I think it's a minor one compared to the above.

Note that a StyleSheetTheme (preferable to a regular Theme, IMO), is actually set from an overridden property on the Page, not by setting the value of the property itself (unless you set it from an HttpModule).

why can't Master pages be applied after Page.PreInit?

Controls determine their IDs and various other characteristics and properties based on their location in the control tree (including things like accessing the form control, etc). A Master Page acts as what amounts to a set of parent controls, so controls can fully initialize themselves until that parent structure is in place. Initialization happens during the Init event, so the Master Page needs to be selected before then.

RickNZ
RickNZ - "StyleSheetTheme is actually set from an overridden property on the Page." I assume this is due to the fact that the earliest we can set StyleSheetThemes programmatically is inside Page.PreInit, which is already too late, since skins defined inside StyleSheetThemes need to be applied to controls prior to PreInit event?!
carewithl