MasterPages: Introduction

MasterPages: Introduction

Paul Wilson

Previous Article            Download Demo            Next Article


MasterPages is the only solution for Page Templates in ASP.NET developed by the Microsoft ASP.NET Team itself, although there are certainly several other Page Template techniques available, including some with server form support. Rob Howard, Program Manager for ASP.NET, speaking about MasterPages, stated "technology similar to the sample will be in the next version of ASP.NET". This article discusses the original MasterPages demo released by Microsoft; see my next article for improvements I've made, including designer support.

What Is MasterPages?

MasterPages is currently a set of server controls, along with a small demo, that is available online in the Control Gallery on the MS ASP.NET website. It is very different than other Page Template methods since it uses controls, instead of a custom base page class that must first be created and compiled. Designers simply create a User Control as the Template, mark it with "Regions", and then add matching "Content" regions in the page "ContentContainer" control. Note that no code needs to be written, its just using the MasterPages controls.

Creating a Template

To create a MasterPages Template you start by creating an ASP.NET User Control and then add your common page layout that you intend all of your pages share. Then you mark any spots in the layout that will be defined in individual pages with a MasterPages "Region" control, along with a unique ID to be matched later. You can include as many Regions as you like, and you can add default content inside Regions that will be used for pages that do not contain a matching ID. Note the Register directive and control syntax is automatic with drag-n-drop.
Listing 1: Creating a Template
<%@ Control %>
<%@ Register TagPrefix="mp" Assembly="MasterPages" 
    Namespace="Microsoft.Web.Samples.MasterPages" %>
  <h1><mp:region id="MPHeader" runat="server">Page Header</mp:region></h1>
<form id="frmMain" method="post" runat="server">
  <mp:region id="MPContent" runat="server">Default Content</mp:region>
  <h2><mp:region id="MPFooter" runat="server">Page Footer</mp:region></h2>

Using the Template

To use the MasterPage Template you start by creating an empty ASP.NET Page, stripping out all the standard content that the wizard automatically adds. Then you add the MasterPages "ContentContainer" control and "Content" controls, with the MasterPageFile attribute of the ContentContainer being the Template, and the IDs of the Content controls matching the various Region control IDs. The only thing you can add to the ContentContainer is matching Content controls, but you can skip some Regions if you prefer to accept their default content.
Listing 2: Using the Template
<%@ Page %>
<%@ Register TagPrefix="mp" Assembly="MasterPages" 
    Namespace="Microsoft.Web.Samples.MasterPages" %>
<mp:contentcontainer runat="server" masterpagefile="Template.ascx">
  <mp:content id="MPHeader" runat="server">Sample Page</mp:content>
  <mp:content id="MPContent" runat="server">Real Content</mp:content>

Why Use MasterPages?

So why should you use MasterPages instead of another Page Template technique? First, its the most flexible, since it automatically supports multiple regions, optional regions with default content, and even nested and repeating templates. Its also the easiest to use, since you just use controls without writing code, which makes it a natural fit into the rest of the ASP.NET page-control model, leaving your developers free to create non-GUI "functional" base page classes. Finally, it will likely someday be the official template method in ASP.NET.

MasterPages Problems

Unfortunately, the current MasterPages controls are just a demo at this time. This means there is almost no design-time support -- you cannot drag-n-drop any controls into the Region or Content controls -- you must add them manually. Your defaults are at least visible for Regions, but you see no Content at all. You will also find this technique is NamingContainer "happy" due to flexibility, but this means your controls will have IDs like Container:_ctl0:Region:Control. These extra control layers also cause this technique to be slower than others.


MasterPages is the most flexible and easiest to use of Page Template solutions, and since it was created by Microsoft it will likely someday be in ASP.NET itself. The original demo suffers from the lack of designer support and poor performance, but my next article will show you how to fix these problems, and add I will also add the ability to define your default content regions and default template files, while also cleaning up the ever-annoying NamingContainer "problem" that occurs. Finally see my website for a demo of making the templates user-selectable as well.

Author Bio

Paul Wilson is a software architect in Atlanta, currently with a medical device company. He specializes in Microsoft technologies, including .NET, C#, ASP, SQL, COM+, and VB. His WilsonWebForm Control allows Multiple Forms and Non-PostBack Forms in ASP.NET. He is a Microsoft MVP in ASP.NET and is also recognized as an ASPFriend's ASPAce/ASPElite. He is a moderator on Microsoft's ASP.NET Forums, as well as one of the top posters. He is certified in .NET (MCAD), as well as also holding the MCSD, MCDBA, and MCSE. Please visit his website,, or email him at

posted @ 2004-02-17 20:13  dudu  阅读(3389)  评论(3编辑  收藏  举报