Master Pages

Hi in this post let me explain what is Master Page in ASP.NET and in couple of days I will also post the similar concept how to do in J2EE and PHP.

It is a template page that can be used as a foundation for any number of ASP.NET content pages in your application. In working with master pages, you create a master file that is the template referenced by a subpage or content page. Master pages use a .master file extension, whereas content pages use the .aspx file extension you’re used to; but content pages are declared as such within the file’s Page

image

Creating a Master Page

image image

Site.master
  1. <form id="form2" runat="server">
  2.         <div id="topContent">
  3.             <h1><center>NPN Training</center></h1>
  4.         </div>
  5.     
  6.         <div id="mainContent">
  7.             <asp:ContentPlaceHolder id="MainContent" runat="server">
  8.             </asp:ContentPlaceHolder>
  9.         </div>
  10.         
  11.         <div id="leftContent">
  12.             <p style="text-align: center;">
  13.                 <asp:Label ID="DateDisplay" runat="server"></asp:Label>
  14.             </p>
  15.             
  16.             <h3>Sessions I</h3>       
  17.             <ul>
  18.                 <li>Intoduction</li>
  19.             </ul>
  20.  
  21.             <h3>Session II</h3>       
  22.             <ul>
  23.                 <li>Master Pages</li>
  24.             </ul>
  25.         </div>
  26.         
  27.         <div id="footerContent">
  28.             <img src="Images/PoweredByASPNET.gif" alt="Powered by ASP.NET!" />
  29.         </div>
  30.     </form>

Now let us create content page Right click the project add new item and from the dialog window select web form.

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server">
    <h2>
        About the Author</h2>
    <p>
        Hello! My name is Naveen
    <p>
        My primary experience lies in Web development with Microsoft technologies.</p>
</asp:Content>

 

Specifying the Title, Meta Tags, and Other HTML Headers in the Master Page

By default, two ContentPlaceHolder controls is created when we create a master page:

The head ContentPlaceHolder enables pages to add custom content to the <head> section.

Code Snippet
  1. <head runat="server">
  2.     <title>Untitled Page</title>
  3.     <asp:ContentPlaceHolder id="head" runat="server">
  4.     </asp:ContentPlaceHolder>
  5.      <link href="Styles.css" rel="stylesheet" type="text/css" />
  6.  
  7. </head>

The purpose of ContentPlaceHolder1 is to define a region in the Web Form that can be customized on a page-by-page basis.

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

<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

</asp:ContentPlaceHolder>

</form>

one named head, and located in the <head> element; and one named ContentPlaceHolder1, placed within the Web Form. The purpose of ContentPlaceHolder1 is to define a region in the Web Form that can be customized on a page-by-page basis. The head ContentPlaceHolder enables pages to add custom content to the <head> section.

Specifying which Master Page to use

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

Specifying which Master Page to use in web.config

<pages masterPageFile="~/Site.master">
            <controls>
                <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
                <add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
            </controls>
            
        </pages>

Specifying the Master Page for a specific folder

<location path="Admin">
        <system.web >
            <pages masterPageFile="~/SiteReplicate.master">
            </pages >
        </system.web>
    </location>

Setting the Page’s Title Declaratively

A content page’s title can be set declaratively through the Title attribute of the <%@ Page %> directive. This property can be set by directly modifying the <%@ Page %> directive or through the Properties window. Let’s look at both approaches.

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

The page’s title may also be set from the Properties window. From the Properties window, select DOCUMENT from the drop-down list to load the page-level properties, which includes the Title property.

clip_image001

Setting the Page’s Title Programmatically

protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = string.Format("{0:d}", DateTime.Now);
    }

Advertisement
Comments
2 Responses to “Master Pages”
  1. Sankar Datta says:

    hello Naveen i need .net material ra…send me as soon as possible k na…dnt forget to send me the material..i need it my dear

  2. Hey naveen………… its very nice to see ur posts

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.