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.
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.
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.
This article has been editorially reviewed by Suprotim Agarwal.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).
Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.
Click here to Explore the Table of Contents or Download Sample Chapters!
Was this article worth reading? Share it with fellow developers too. Thanks!
Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of
DotNetCurry,
DNC Magazine for Developers,
SQLServerCurry and
DevCurry. He has also authored a couple of books
51 Recipes using jQuery with ASP.NET Controls and
The Absolutely Awesome jQuery CookBook.
Suprotim has received the prestigious Microsoft MVP award for ten consecutive times. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.
Get in touch with him on Twitter @suprotimagarwal or at LinkedIn