NCS Logo - Click for home page Northstar Developer Center
Platforms
All Platforms
.NET Framework (1.x - 4.x)
Active Server Pages
ASP.NET
C#
SQL Server
VB.NET
Visual Basic

Keywords
.NET Data Types
.NET E-mail
.NET Events
.NET Functions
.NET Object Programming
.NET System.Configuration
.NET System.Diagnostics
.NET System.IO
.NET System.Net
.NET System.Net.Sockets
Active Data Objects
ASP Architecture
ASP Black Belt
ASP Built-in Functions
ASP Built-in Objects
ASP Debugging
ASP Performance
ASP Security
ASP Syntax
ASP.NET Authentication
ASP.NET Controls
ASP.NET Data Access
ASP.NET Features
ASP.NET Master Pages
ASP.NET Page Events
ASP.NET Security
ASP.NET ViewState
Atom
Certifications
COM, DCOM, COM+
Data Access
E-Mail
Errors
Exporting Data
HTML Tips
IIS
Object-Oriented Programming
RSS
SQL
Uncategorized ASP Tips
VB API Programming
VB Forms
VB Syntax
XML

Book Support
Visual Basic 6 Bible
ASP Bible
ASP Weekend Crash Course
ASP.NET At Work
Creating Web Services

Dynamic Master Pages

Written by Eric Smith, Northstar Computer Systems LLC

Well, my ASP.NET 2.0 content management system seems to be working well, and I've already converted three of my managed sites to use it. I've found a few things that might be helpful if you're doing similar work with dynamically assigned master pages.

To start with, assigning the master page to a page is pretty easy. It's done in the OnPreInit event handler, as shown here:

protected override void OnPreInit(EventArgs e)
{  
base.OnPreInit(e);  
base.MasterPageFile = somefilename;
}

I have this in a BasePage class that all my web pages inherit from. This saves me from having to duplicate the code everywhere.

Another thing I do in the BasePage is set the page title. However, there's a trick to this, depending on what event you do it in. Each of the pages for the CMS are assigned to a template to display them. Each template inherits from BasePage, which takes care of assigning the master page to it. In the OnLoad event of the template page, I set the Title property of the page. However, I want each page's title to be prefixed with something else.

The OnLoad event of the BasePage class takes the Title set by the child page and uses a format string to assign it to the page. If you get the events out of order, the BasePage doesn't effectively set the page title. In my example, the OnLoad of the child page sets just the 'subtitle', and the OnLoad event of the BasePage uses that Title and prepends it. That seems to work well.

One other recommendation when working with dynamic master pages: have a default master page. When you're editing your ASPX pages, you can use the asp:Content tag only if a master page is set in the @Page directive. Since I'm dynamically assigning the master page, I had been leaving out the MasterPageFile directive. The result of this is that the site won't build properly when you choose to Publish the site. My solution is to pick one of the MasterPageFiles for each template page and then remove it when I put the template on the host server. You can probably leave it in with no problem since it's being replaced, but I'm still experimenting with that.

I'm definitely sold on master pages and I'm in the process of converting all my 1.1 sites to that format. It's much less of a hack than all the other options I've implemented for 1.1 sites to provide a common look-and-feel. Microsoft definitely learned from their oversight in 1.0/1.1 with this feature.

Keywords: [ ASP.NET Master Pages | ASP.NET Page Events ]

Publication Date: 3/1/2006, Last Update: 3/22/2010