views:

321

answers:

1

I have a masterpage which is set via a HTTPModule on PreInit(). HAPPY TIME! Problem is I need to override the masterpagefile value on a few pages due to a layout issue. Anyone know the best way?

I tried adding a Page_Preinit on my page, but it is executed before the PreInit() in my module, so it ends up being reset there. I'm thinking maybe I should update a context variable in my page (something like masterSetAtPage which I might branch off of in my module).. any thoughts on that?

I'm working in VB.Net, but a C# example would be fine also.

+1  A: 

Presumably your current code looks a little like the code described here...

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

If so, then you can change your code that hooks up the PreInit to look something like this...

if (page != null && String.IsNullOrEmpty(page.MasterPageFile))
{
    page.MasterPageFile = "~/Master1.master";
}

and then any page that uses it's own page directive to set a masterpagefile will avoid being hooked up to the "default" master page.

If you need to do this programatically, then this code...

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);
    this.MasterPageFile = "cheese.master";
}

... in your Page's code behind would do the trick. Obviously this code would only be added to pages where the special behaviour was needed.

Martin Peck
That looks good, however, I actually need to programmatically set the master, so I can't set a static value in the page directive.
madcolor
OK, so doing this...protected override void OnPreInit(EventArgs e){ base.OnPreInit(e); this.MasterPageFile = "cheese.master";}... in your page should work. I'm aware that this is pretty much what Mark suggested (above) - although he showed OnInit and not OnPreInit.
Martin Peck
@Martin - yep, I pooched that one. ;) PreInit is the right place.
Mark Brackett