How to create and use Master Pages in ASP.NET 2.0


A Master Page enables you to store the same content among multiple content pages in a website. You can use Master Page to create a common page layout. For example, if you want all the pages in your website to share a three-column layout, you can create once in a Master Page and apply the layout to multiple content pages.
You also can use Master Pages to display common content in multiple pages. For example, if you want to display a standard header and footer in each page in your website, then you can create the standard header and footer in a Master Page.

With the help of Master Pages, you can make your website easier to maintain, extend and modify.

You create a Master Page by creating a file that ends with the .master extension. You can locate a Master Page file in any place within an application. Furthermore, you can add multiple Master Pages to the same application.
Creating Master Pages: add a master page in visual studio 2005

MasterPages1.gif
 
Listing 1.1 MasterPage.master

<%@ 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>Master Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:contentplaceholder id="Header" runat="server">

            <asp:Label ID="Label1" runat="server" Text="Header..." Font-Bold="True" Font-Italic="False" Font-Names="Verdana" Font-Size="20pt"></asp:Label></asp:contentplaceholder>

    </div>

    <br />

    <div>

        <asp:contentplaceholder id="Content" runat="server">

        </asp:contentplaceholder>

    </div>

    <br />

    <div>

        <asp:contentplaceholder id="Footer" runat="server">

            <asp:Label ID="Label2" runat="server" Text="Footer..." Font-Names="Verdana" Font-Size="15pt"></asp:Label></asp:contentplaceholder>

    </div>

    </form>

</body>
</html>

There are two special things about the Master Page in Listing 1.1 First, notice that the file contains a <%@ Master %> directive instead of the normal <%@ Page %> directive. Second, notice that the Master Page includes two ContentPlaceHolder controls. When the Master Page is merged with a particular content page, the content from the content page appears in the areas marked by contentplaceholder controls. You can add as many ContentPlaceHolders to a Master Page as you need.


Using a Master Page:


Add a page in the visual studio 2005 to use Master Page.

MasterPages2.gif
 
Select the name of Master Page to bind with that page.


 MasterPages3.gif


Listing 1.2 UsingMasterPage.aspx

<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="UseMasterPage.aspx.cs" Inherits="UseMasterPage" Title="Untitled Page" %>

 

<asp:Content ID="Content2" ContentPlaceHolderID="Content" Runat="Server">

    <asp:Label ID="Label1" runat="server" Text="Content Section From Another Page"></asp:Label>

</asp:Content>
The listing 1.1 & 1.2 will give the following output.

MasterPages4.gif 
I think this will help to beginners to know about the Master Pages in asp.net 2.0.

Up Next
    Ebook Download
    View all
    Learn
    View all