OK I’ve got a master page and a nested master page that inherits that master.
All pages implement the nested master page:
MasterPage
--> NestedMaster
--> Page1
--> Page2
--> etc.
So I need to set the top level master page dynamically. 2 sites with completly differing layouts in the header and the header only.
How can I control this in code? Looked at the event hierachy but this just says you can only set the master page at page level, I’ve got thousands of pages! This is not practcal.
I want to make a single change at the NestedMaster page level and that level only. Can this be done?
I’ve tried:
protected override void OnInit(EventArgs e)
{
this.MasterPageFile = getMasterPath();
base.OnInit(e);
}
but I just get:
The ‘MasterPageFile’ property can only be set in or before the
‘Page_PreInit’ event.
The master page has no PreInit event, annoyingly!
Having done a bit more googling I realise this isn’t possible becuase the base class is the
Pageand this is the first thing called and therefore the only place you can specify the PreInit. God I hate webforms.That said I think I’ve got a solution from http://odetocode.com/Articles/450.aspx. Basically produce a custom HTTPModule: