Create new account I forgot my password    

ASP.NET 2.0 - A Quick Introduction To Master Pages - Beginner Level
Rating: 27 user(s) have rated this article Average rating: 4.3
Posted by: Suprotim Agarwal, on 10/20/2007, in category "ASP.NET 2.0 & 3.5"
Views: this article has been read 22983 times
Abstract: Master pages are one of the cool feature offerings in ASP.NET 2.0 which lets you define a consistent look and feel for the entire site. In this article, we will take a brief note of what master pages are and how to use them in your site. I assume that you have a basic familiarity with asp.net 2.0

A brief note on Master Pages
Master Pages are a means of providing a consistent look and feel to the application. A very common requirement that is encountered while developing a website is to have consistent header and footer throughout all pages. One way to do this in the past was to create User Controls and place them on the pages to give a consistent look throughout the website. ASP.NET 2.0 introduces the concept of Master Pages to satisfy this requirement.
A Master Page is a template based on which all other pages are derived. A Master page consists of the master page and one or more content pages. You create the content pages as you would usually do containing your content. When the page is requested, the master and content pages are merged together, thereby giving the consistent look.
Some important points to note about master pages would be the following :
1.    A master page is an asp.net file with the .master extension
2.    A master pages consists of one or more <asp:contentplaceholders>. These are sections where your content appears.
How do I create a master page?
It’s quiet simple. Just create an asp.net website as you would usually do (File > New> Website). Right click on the Project in the Solution Explorer (Ctrl + Alt + L) and click on ‘Add New Item’. In the Add New Item dialog, choose ‘Master Page’ and click Add. The MasterPage.master appears in the solution explorer. If you examine this master page, the contents would look similar to the following :
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>
If you chose VB.NET as your language, the only difference will be in the <%@ Master> directive. It would like this :
<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>
 
On examinining the contents of this master page, you will find the <asp:contentplaceholder>. This is where the content of your content pages appears.
Let us add a blue header to this page which we would like to appear on all pages. Switch over to the designer with the MasterPage.master selected. Drag a Panel from the toolbox over the ContentPlaceHolder (make sure you place it over the ContentPlaceHolder and not inside it). Rename the Panel to panelHeader and change its Back Color property to ‘SteelBlue’. Also change the Width of the panel to 100%. Inside the Panel, type the text ‘My First Master Page’.
Your code finally would look like this :
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Panel ID="panelHeader" runat="server" BackColor="SteelBlue" Height="50px"
            Width="100%">
            My First Master Page</asp:Panel>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>
 
Ok so that was a small template that we created as shown below.
 Master Page
Let us create a content page to see how this template is applied to the page. We want this blue colored header to appear in all our pages.
So right click on the Project in the Solution Explorer and click on ‘Add New Item’. In the Add New Item dialog, select the ‘Web Form’. Also make sure that you have selected the ‘Select Master Page’ checkbox on the bottom. On clicking Add, you will see a ‘Select a Master Page’. Choose the MasterPage.master that we created earlier and click Ok. The Default1.aspx page appears in the solution explorer which looks similar to this :
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default1.aspx.cs" Inherits="Default1" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
 
In vb.net this would look like :
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default1.aspx.vb" Inherits="Default1" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>
If you have observed quickly, on switching to the ‘source’ view in your visual studio, you will see that this page does not contain the html, body and other tags that would usually appear on any other asp.net page. Yes you guessed it correctly. In the beginning of the article, I mentioned that when the page is requested, the contents of the Master and Content Pages are merged together. This page gets its header and body tags from the Master Page. The page is linked to the master page using the MasterPageFile attribute that you can see in the <%@Page> directive.
Placing your content inside this content page would be just like you design any other page. Switch to the designer of the default.aspx page and place a button and a text control on to the Content container.
Switching back to the code view, you see the following :
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <asp:Button ID="Button1" runat="server" Text="Button" />
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</asp:Content>
Run the application by pressing F5 and you will see your page along with the Blue header that you created earlier in your master page as shown below.
ContentPage
Similarly you can create multiple content pages in your site, each having different content however a similar layout (the same blue header), which ofcourse is provided by the MasterPage. 
This whirlwind tour of Master Pages has given you an idea of how to use them to apply a consistent look and feel to your entire site. You will have to use your creativity to make maximum use of this wonderful feature. I hope this article was useful and I thank you for viewing it.









Page copy protected against web site content infringement by Copyscape


How would you rate this article?

User Feedback
Comment posted by Suneel.G on Thursday, November 01, 2007 8:03 AM
Its very usefull one.
Comment posted by vasu on Friday, November 02, 2007 5:50 AM
Hi,

This is very nice article.Please continue like this

Thanks a lot

vasu
Comment posted by vimal on Saturday, December 08, 2007 2:13 AM
please send more article like this. These all very usefull and easy to read.
        Thanks a lot,
Comment posted by Suprotim Agarwal on Monday, December 10, 2007 7:31 AM
Hi Vimal, Thanks. I will post some more articles soon.
Comment posted by Appu on Monday, February 04, 2008 1:42 AM
Very Simple and Nice Article.
Comment posted by Maggie on Wednesday, February 20, 2008 9:03 PM
Excellent doc, simple and clear, easy to follow!
Thank you for this post
Comment posted by Naresh on Wednesday, April 02, 2008 6:14 AM
Hai,It;s Good Article..
Comment posted by Lizet on Friday, April 11, 2008 11:14 PM
i'm trying to use java script to show an alert in my web page. I write some java script code in content place holder to show an alert when i click button.   It's work, but the alert makes my web appearance messy. Can u help me to solve my problem? How to make java script work well in the master page which use ccs?
Comment posted by Suprotim Agarwal on Monday, April 14, 2008 10:05 AM
Hi Lizet, Take a look over here
http://www.velocityreviews.com/forums/t122357-javascript-and-masterpages.html
Comment posted by ravi on Tuesday, July 01, 2008 11:40 PM
hi nice thiught very usefull to every one please read this one
Comment posted by vivek tiwari on Monday, July 14, 2008 11:28 PM
i wnat to know that how diffrent searching techniques apply for a project.
Comment posted by amit.seedhar on Thursday, August 07, 2008 6:29 AM
Hi,

This is very nice article.Please continue like this

Thanks a lot
Comment posted by Vuyiswa Maseko on Sunday, August 10, 2008 3:17 PM
hi Suprotim

When i buy a book, but there are parts of chapters that are very simple, but cant understand until i read Articles like yours. thanks man its helpfull.

i gave u 5

Vuyiswa Maseko
Comment posted by Suprotim Agarwal on Tuesday, August 12, 2008 5:57 AM
Ravi, Amit, Vuyiswa: Thanks for the kind words!!
Comment posted by Vuyiswa Maseko on Sunday, August 31, 2008 6:34 PM
You welcome and you deserve them :)
Comment posted by Mike J. on Tuesday, September 09, 2008 10:47 AM
Your articles are very helpful to me in learning ASP.NET, this article and others!  Thank you!  Do you have an Advanced version of this topic on Master Pages?  Can a label on a MasterPage.master be changed programmatically by sub-pages (content pages)?
Comment posted by Suprotim Agarwal on Tuesday, September 09, 2008 9:26 PM
Mike J: Thanks for the feedback. I will be writing one on MasterPages very soon. Subscribe to my RSS feed to stay in touch when the latest articles are released here.
Comment posted by Anuja on Thursday, November 06, 2008 7:35 AM
Thanks for open heart to teach us Master Pages
Comment posted by dhiraj kurde on Thursday, March 19, 2009 6:09 AM
As I am a beginner, your article is very useful to me.
thanx..
plzzzzzzzz keep it up.
Comment posted by Seraj on Saturday, May 23, 2009 3:24 AM
Realy Wonderfull post ever I have
Comment posted by Mir Hassan Zardari on Thursday, October 15, 2009 9:53 AM
Yar this site which helps us how to remove our mistakes, is wonder ful
Comment posted by virendra maurya on Friday, March 19, 2010 2:31 PM
i'm trying to use java script to show an alert in my web page. I write some java script code in content place holder to show an alert when i click button.   It's work, but the alert makes my web appearance messy. Can u help me to solve my problem? How to make java script work well in the master page which use ccs?
Comment posted by Suprotim Agarwal on Monday, March 22, 2010 11:06 PM
Virendra: Check this article http://www.dotnetcurry.com/ShowArticle.aspx?ID=273
Comment posted by mvvs sarma on Friday, May 14, 2010 10:45 PM
very nice article, and is very useful.

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel

NEWSLETTER