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

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" "">


<html xmlns="" >

<head runat="server">

    <title>Master Page</title>



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


        <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>


    <br />


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



    <br />


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

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




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.

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


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>

The listing 1.1 & 1.2 will give the following output.

I think this will help to beginners to know about the Master Pages in 2.0.