Override OnPreInit on a Page with Master Page

I’m doing one of the most complicated UI I’ve ever did(ASP.NET). In the process, I need to generate tabs(via MultiView control) pragmatically and for each tab render a tree-like checkboxes. All based on external xml. Yami.. ;-)


Reading a little about how to add dynamic View to MultiView control made me realize that I need to do my magic on the page’s PreInit event. This one was quite easy, I’ve override the OnPreInit (I could register to the event in the page constructor, but this is easier) and called my dark voodoo “BuildTabs” method. All is good.


Now, Adding MasterPage to the blend made me puzzled for a few minutes. It seems that the page’s controls are not initialized on the PreInit phase if the page has MasterPage. After I got my eyes back on the screen, stop nodding with disappointment and taking my head off the wall, I used my built-in behavioral program:


pre-think (short phase ~Thread.Sleep(1000*60));
while(no solution yet) { 
   try;
   think;
}.


2 minutes later I found the trick:


protected override void OnPreInit(EventArgs e)
{
   base.OnPreInit(e);
   base.Master.Init += new EventHandler(Master_Init);
}

void Master_Init(object sender, EventArgs e)
{
   BuildTabs(); //we must draw it from scratch on each post-back.
}


Now the controls are initialized and I can program in peace.

 

Oren Ellenbogen

 

2 thoughts on “Override OnPreInit on a Page with Master Page

  1. Just a small note about the registration to the PreInit event: Is there a reason you’re not using the AutoEventWireup property? It means that in order to sign up for the event you just have to write:

    protected void Page_PreInit(…)

    And the page will sign you up automatically.

  2. Hey Doron.
    I’m familiar with the solution. I guess that I’m used to override the required method or explicitly register to the event(no special reason by the way). But I’ll make the change as I believe your suggestion will make the code clearer. Thanks.

Comments are closed.