I am working with a third party asp.net application that uses master pages and nested master pages. My needs are to dynamically set the master page files for each page(.aspx). The application by default sets the master page file in the strongly typed @Page directive for each page. I don’t want to change the strongly typed directive on each page (over 50 pages) because I am lazy and I want to minimize conflicts with future upgrades.
My solution was to use the base masterpage class and override the OnPreInt event like this:
protected override void OnPreInit(EventArgs e)
{
this.MasterPageFile = "~/MasterPages/MyMaster.master";
}
Everything works perfectly. My question is: Is this a bad idea and why? It just seems too easy to be true.
thanks.
It’s a perfectly good idea. Half the point of master pages is that you can do this.
One annoying thing about them, is that you can’t have them start referencing a non-existent file (which would make it clearer when you’re always going to decide on the master programattically), so if I’m going to always set it to something new I like to have it start with a page that just says “Dummy master page, this will be moved from programatically”, so it’s clear that this is happening to anyone tracking down the masterpage used by the page.