Click here to Skip to main content
15,881,172 members
Articles / Web Development / ASP.NET
Article

Master Pages in ASP.NET 2.0 (Part 1)

Rate me:
Please Sign up or sign in to vote.
3.26/5 (18 votes)
14 Nov 20056 min read 102.4K   1.2K   54   17
An article on implementing Master Pages based templates in ASP.NET 2.0.

A Template Driven Website

Introduction

If you are a Microsoft Web Developer, I bet, you have, in more cases than one, found yourself either looking for an out-of-the-box framework which allows you to create templates to maintain a consistent look and feel with easy navigation in your website, or you have just developed something which allows you to do this yourself. This article introduces web developers to a new feature called "Master Pages" which comes with ASP.NET 2.0 and provides a standardized way to have templates incorporated in your website so that you don't have to re-invent the wheel.

Background

Typically in the past if Microsoft web developers like us needed to develop a web site which had a consistent look and feel and content which changed from page to page, we resorted to different techniques which allowed us to achieve something close to a template driven solution. Classic ASP developers resorted to Server Side Includes, ASP.NET 1.1 developers used User Controls, and Web Designers used third party solutions like Macromedia Dreamweaver templating which was a client side solution and involved use of a tool to update all HTMLs which use a particular template when the template was updated.

With "Master Pages" in ASP.NET 2.0 and Visual Studio .NET 2005, Microsoft gives an out-of-the-box standardized way for achieving the same results and at the same time enjoying complete integration with the Visual Studio .NET IDE and WYSIWYG development! In this article we take a dive into Master Pages and introduce ourselves to a Hello World project using Master Pages!

The article that follows will describe additional code concepts like exposing properties from Master Pages which allows modification of Master Page Controls from within Content pages and using themes, but for now let's get started with the Master Page basics!

Getting Started With Master Pages

The first step to getting started with ASP.NET Master Pages is to create a "Blank site" in your Visual Studio .NET 2005 IDE. Once a blank project has been created right click the project and add a new item. From the list of item types that you can create, choose a "Master Page" to create a new master page. The following screen shot illustrates this:

Image 2

Master Pages (files with ".master" extension) are templates which will provide a "structure" to the content pages. A typical case which can be used to describe master pages is a case where the left navigation page and the top part of the website remains fixed for all pages while the content part keeps changing depending on the link the user clicks. In this example, the top panel and left panel would typically be implemented in a master page. The part that keeps changing from link to link will be implemented as a content place holder control in the master page. The following screen shot illustrates a simple master page design that I created where the site header and navigation panel will be fixed for the entire site but the content will keep changing from page to page:

Image 3

Another thing to note here is that a Content Place Holder control is automatically created by Visual Studio which allows you to define the default content for that content area. This is useful if a "Content Page" which uses this template does not provide any content for this area. In other words this content will be used if the content page using this template does not override the content.

Once I've laid out my basic site design in the master page and decided which sections are a part of a template and which sections keep changing from page to page, I move ahead and start defining my content pages. To add a content page for a master page, I just right click the master page and click "Add Content Page". Once I've added a new content page, a new "aspx" file is created for me. As mentioned earlier, an advantage of using Master Pages is that it seamlessly integrates with the Visual Studio IDE. Essentially, one advantage I get is while editing Content Pages, I still get to see the "whole picture" of my website but I am not allowed to modify parts that belong to the template. As the following screenshot illustrates, while developing my content pages, I can still see the parts that are being extracted from the master page (the top and the left panel) but these are grayed out and I am not allowed to modify these while working on the content page!

Image 4

We follow the same procedure to create another content page based on the master page. Once we've created a content page for the "Other Links" page (in the attached project I called this MyLinks.aspx) by using the same procedure described above, we modify the master page to update my navigation panel hyperlinks. In the screenshot that follows I've just updated my master page with a link using an <a> </a> tag to point to my newly created content page. The link "Home" is made to point to the first content page (Default.aspx) that I had created and "Links" now points to MyLinks.aspx. The following screenshot illustrates the changes made to the master page; where essentially just two static pieces of text on the navigation panel are now hyperlinked to content pages.

Image 5

Once this is done and the changes are saved, these changes are instantly reflected on my content pages! As the following screenshot illustrates, I am modifying Default.aspx, which is a content page but all changes made to the master page or the template are automatically reflected in the content page!

Image 6

Looking Under the Hood

Taking a peek at how Visual Studio generates the code for master pages is usually helpful in better understanding how Master Pages work. A lot of this will be covered in the second part of this article that goes into the advanced concepts of Master Pages but here are a few basic concepts.

When you create a master page, Visual Studio automatically inserts a content place holder for the master page. As the 'default content' to the content place holder the source code of the master page is modified and new code is generated inline. The following code snippet illustrates this:

HTML
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
  <span style="font-size: 10pt; font-family: Tahoma">This is the default content. 
      It shows up when there is no content to override this content.</span>
</asp:contentplaceholder>

The content files on the other hand are just responsible for providing content and referencing the master page. Thus, the link between which content page uses which master for its layout is maintained. The following code snippet illustrates this:

ASP.NET
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" 
         AutoEventWireup="true" CodeFile="Default.aspx.cs" 
         Inherits="_Default" Title="Untitled Page" %>
<asp:Content ID="Content1" 
     ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
 <span style="font-size: 10pt; font-family: Tahoma">
       This is the content of my first Page!!!</span>
</asp:Content>

In the above code snippet, the MasterPageFile attribute of the Page tells .NET which master page is being used for this content page. It must be noted here that when a user visits the website, he will essentially be navigating to the content pages (aspx files). So, from a broader prospective, the aspx pages or content pages are actually responsible for extracting the code from master pages, rendering the layout of the master page HTML and then merging the content HTML in the correct places. This concept takes a little time to getting used to but is particularly useful while debugging problems with content pages.

Points of Interest

This is something we've all been doing in the past using customized technologies but it's really exciting to have a "Microsoft Approved" out-of-the-box solution to address this problem a web developer faces almost everyday in his life!

History

Initial draft. All comments / suggestions are welcome at rajivpopat@hotmail.com.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
India India
Rajiv has been developing on Microsoft Platforms for over 6 years and is a principal officer, technology @ eFORCE Inc. where he has worked for around 5 years and enjoys doing some really interesting stuff like writing a software adaptor for MFD scanners and other hardware using Microsoft Technologies, Writing a DAL that hooks up VC++ layers to .NET, writing a .NET Remoting Bridge that securely connects world-wide oil rigs of a drilling company and many other interesting endeavours.

More information about Rajiv can be found @ www.rajivpopat.com

Comments and Discussions

 
GeneralAJAX Pin
Faiz VP23-Nov-05 7:32
Faiz VP23-Nov-05 7:32 
GeneralRe: AJAX Pin
rajivpopat23-Nov-05 13:49
rajivpopat23-Nov-05 13:49 
GeneralRe: AJAX Pin
Jaret12-Jan-06 13:18
Jaret12-Jan-06 13:18 
GeneralRe: AJAX Pin
rajivpopat16-Jan-06 5:24
rajivpopat16-Jan-06 5:24 
GeneralRe: AJAX Pin
rajivpopat8-May-06 21:43
rajivpopat8-May-06 21:43 
QuestionRe: AJAX Pin
WaleedS1-Nov-06 22:56
WaleedS1-Nov-06 22:56 
AnswerRe: AJAX Pin
rajivpopat2-Nov-06 23:48
rajivpopat2-Nov-06 23:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.