Professional Documents
Culture Documents
NET
Rahul Rajat Singh, 21 Feb 2012
Introduction
This article is a beginner's tutorial on understanding Master pages and implementing master pages
in ASP.NET.
Background
Coming from an application development background, when I started working on web development,
the first thing I noticed was that all ASP.NET pages in our website required designing separately.
Unlike Windows application, there is no way we can define common UI layout for ASP.NET pages. But
this understanding of mine was short lived as I came to learn about Master pages in ASP.NET. Using
master pages, I can actually create the common UI elements for all the web pages and create a
consistent look and feel for my whole website. (Dreamweaver also provides a similar feature in the
form of Templates).
Consistent UI is the key to good user interface. Before Master pages, the way we created consistent
web pages was by using custom controls, CSS and JavaScript. With master pages, ASP.NET relieves
the developer from the burden of creating the consistent web pages. Master pages not only provide
the mechanism for creating consistent web pages, but also give us a centralized way of changing the
UI elements that need to be changed across all the pages of the website.
To visualize the Master page - Content page relationship, let's look at the following picture:
Note: This image has been taken from www.asp.net.
We will be looking at how to use master pages in our website to provide consistent UI for our
website.
Creating a set of controls that are common across all the web pages and attaching them to all the
web pages.
A centralized way to change the above created set of controls which will effectively change all the
web pages.
Dynamically changing the common UI elements on master page from content pages based on user
preferences.
Terminology
Let us look at the basic terminology that needs to be understood before jumping into master pages:
Masterpage: Gives us a way to create common set of UI elements that are required on multiple
pages of our website.
ContentPage: The ASP.NET web page that will use master page to have the common UI elements
displayed on rendering itself.
ContentPlaceHolder: A control that should be added on the MasterPage which will reserve the
area for the content pages to render their contents.
ContentControl: A control which will be added on content pages to tell these pages that the
contents inside this control will be rendered where the MasterPage's ContentPlaceHolder is
located.
Creating a MasterPage
To create a master page, we need to:
Now let's look at the stuff that is important. When we look at the MasterPage, we will see
that masterpage has a ContentPlaceHolder control. All the code that is common for the content
pages is outside the ContentPlaceHolder control (in our case, a simple menubar).
<div align="center">
<h1>My Test WebSite</h1>
<asp:Menu ID="Menu1" runat="server" BackColor="#B5C7DE" DynamicHorizontalOffset="2"
Font-Names="Verdana" Font-Size="0.8em"
ForeColor="#284E98" Orientation="Horizontal"
StaticSubMenuIndent="10px">
<StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<DynamicHoverStyle BackColor="#284E98" ForeColor="White" />
<DynamicMenuStyle BackColor="#B5C7DE" />
<StaticSelectedStyle BackColor="#507CD1" />
<DynamicSelectedStyle BackColor="#507CD1" />
<DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
<Items>
<asp:MenuItem Text="HOME" Value="HOME" NavigateUrl="~/Default.aspx">
</asp:MenuItem>
<asp:MenuItem Text="ABOUT" Value="ABOUT" NavigateUrl="~/about.aspx">
</asp:MenuItem>
<asp:MenuItem Text="CONTACT" Value="CONTACT" NavigateUrl="~/contact.aspx">
</asp:MenuItem>
</Items>
<StaticHoverStyle BackColor="#284E98" ForeColor="White" />
</asp:Menu>
<!--<span class="code-comment"> Here we have content place holder where all content pages
will render their controls --></span>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
Once we have all the master pages and content pages ready, we can test run our website.
So the common set of controls and behavior has now been added to all the content pages. We can
have any number of controls and functionality on the master pages.
Let us, in our small test website, have a label in master page that will tell the user about the current
page. Let's think of this label as breadcrumbs (just to illustrate there is no way, breadcrumbs should
be created like this).
This label will display the string that is set by the Content page so we need to have
a public property in the master page to accomplish that.
Now we can use this property to set the PageName property of master page from content pages.
One important thing to mention here is that if we want to use this property from our content page,
then we need to have a @Mastertype declaration in our content pages. This declaration will enable
the content pages to use the public properties of Master Pages.
and from our content pages, we can simply set the property of the master page.
//default.aspx
protected void Page_Load(object sender, EventArgs e)
{
this.Master.PageName = "/Default.aspx";
}
//about.aspx
protected void Page_Load(object sender, EventArgs e)
{
this.Master.PageName = "/About.aspx";
}
//contact.aspx
protected void Page_Load(object sender, EventArgs e)
{
this.Master.PageName = "/Contact.aspx";
}
and now if we run the website, we can see the label on master page showing the appropriate page
name which is being set by the content page.
Changing the Master Page Dynamically
Now there are scenarios when we want the user to change the layout of the website. Since our
website's layout is defined by the masterpage, to accomplish this we need the ability to change the
master pages dynamically from the code.
So to do this, let us add one more master page to the website. This master page will have the menu
vertically aligned on the left side unlike the first which has it on top.
Now to let the user change the MasterPage:
We need to have the option of changing the master page - let's have a new page change.aspx, we
will place radio button on this page to be able to change master page for this page.
We need to save the current selected master page - let's save it in a session variable.
Change the master page on the fly - We need to do it in Page_PreInit of content
Page Change.aspx.
//global.asax
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Session["master"] = "~/MasterPageOne.master";
}
//change.aspx
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack == false)
{
RadioButtonList1.SelectedValue = Session["master"].ToString();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Session["master"] = RadioButtonList1.SelectedValue;
//lets reload to see the change
Response.Redirect("Change.aspx");
}
Points of Interest
Now let us try to sum up what we have seen so far by doing this small unrealistic example.
There is also a possibility of having nested master pages if we carefully use MasterPage inside
the ContentPlaceHolder of another master page thus exposing a new ContentPlaceHolder of
nested MasterPage for content pages to use.