Search code examples
.netasp.netmaster-pageshttpmodule

masterpage module override


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.


Solution

  • 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.