qt8gt0bxhw|20009F4EEE83|RyanMain|subtext_Content|Text|0xfbffd70000000000d800000001000100
One of the cool new things introduced in ASP.NET 2.0 is Master Pages. Master Pages give you the ability to define a master page layout and look that is used throughout a site to give a consistent look & feel to all pages. Any updates or changes to the look & feel of the site is done in only one place - the Master Page. Something that might be useful is the ability to dynamically load a master page at runtime, for example based on user preferences or to implement a simple skinning ability for a site framework that might host multiple different sites (something like .Text). It is not a difficult task and can be done very generically to make for easy reuse.
The master page can be selected for a page easily at design-time, I won't focus on that here. But assigning a master page at run-time can only be done in the OnPreInit event since the rendering of the page with the master page occurs prior to the Init event. However, in the case where you want to load the master page dynamically, for example from a database entry based on a user's preference or something, you don't want to add code to the OnPreInit event on every page in the site. It is easy enough to add that code to a base page class that you can inherit from for you pages in the site.
public class DynamicPage : System.Web.UI.Page
{
protected override void OnPreInit(EventArgs e)
{
string masterfile = getMasterPageFromDatabase();
if (!masterfile.Equals(string.Empty))
{
base.MasterPageFile = masterfile;
}
base.OnPreInit(e);
}
}
Now, all you have to do is inherit from that class for all the pages in the site. But this is where it gets even cooler. I posted before about some cool changes for the web.config file in ASP.NET 2.0. So now, you don't even have to change each page class in your web application to inherit from our new DynamicPage class. You can just add it into the web.config and all pages will automatically inherit from it. Doesn't matter if it is a page in the project that you have already, or a new page you add later, it will know to inherit from the DynamicPage class. You add the following to the web.config:
<system.web>
<pages pageBaseType="DynamicPage" />
<!-- ...rest of system.web items... -->
</system.web>
Make sure to include the fully-qualified name, including namespace(s) if appropriate, but that is it. I did a quick test and threw a couple of master pages together to test it out. I also added a property to my DynamicPage class to allow for setting the master page file at runtime from a page that inherits from the class and I added a dropdown to allow the master page to be selected by the user. The property I added just stores the master page file name and reloads the current page so the PreInit would fire again, nothing too fancy. Here's some quick images of the pages with the different master pages applied to it (as selected by the user at run-time)
Gives a quasi-skinning feel to the site, none of the content pages need to change - and took me all of about 5 minutes to do. All you need to do is implement separate master pages for each look - which is brain-dead easy. I didn't make my master pages anything fancy at all, but changed enough to demonstrate a different look and layout for each master. Don't forget that this does not replace other good design habits, you can still implement the parts that make up the master page as user controls so you're not building things more than once. You can use this same idea with ASP.NET 2.0 themes to load themes for controls at run-time also.
Edit
My take on master pages is that they are very cool and will be a huge benefit to have in ASP.NET 2.0. That said, I do still plan on implementing traditional skinning for most of my own sites & frameworks. I like skinning and having the real granular level control for everything on the page/site. Master pages will be great for a quicker implementation or where simpler requirements permit. I still love regular web application skinning.