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.