Register | Login
Thursday, August 28, 2008

Sections
  
About Us
  
Hosting Provided by Server Intellect
Partners
Downloads
  
 WWWCoder.com Resource Directory

Introduction to ASP.NET Master Pages
6/28/2006 12:26:49 PM

With the inception of ASP.NET 2.0 with VS.NET 2005, a lot of new features have been introduced which make it possible to increase productivity and web application design very easy. One of these features is master pages. Master Pages are the templates which can be used as the foundation for numerous ASP.NET content pages. With this feature of Master Pages, the productivity has been increased and also makes it easier to manage ASP.NET 2.0 applications after they are built.


With the inception of ASP.NET 2.0 with VS.NET 2005, a lot of new features have been introduced which make it possible to increase productivity and web application design very easy. One of these features is master pages. Master Pages are the templates which can be used as the foundation for numerous ASP.NET content pages. With this feature of Master Pages, the productivity has been increased and also makes it easier to manage ASP.NET 2.0 applications after they are built.

To Add a Master Page in the application follow the steps:

1: Right Click Solution Explorer | Select Add New Item. This will give the following dialog box (see Figure 1):

Add New Item Dialog

Figure 1 - Add New Item Dialog

2: Select Master Page and click Add. Master Page will be added to your website (see Figure 2):

Master Page Added to Site

Figure 2 - Master Page Added to Site

Master Pages have .master extension, where as content pages have .aspx extension.

As a developer one can put anything in the master page that one wants to share throughout the application. This can include header, footer, navigation etc. which to be used across the entire application. The content page then contains all the page content except the master pages’ elements.

The Source view of Master Page:

<%@ Master Language = "VB" CodeFile = "MasterPage.master.vb" 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>

To add a content page, just right Click the Master Page and select Add Content Page (see Figure 3). Alternatively you can follow the following steps:

Adding a Content Page

Figure 3 - Adding a Content Page

Right Click the Solution Explorer and select Add New Item (see Figure 4)…

Add New Item Option

Figure 4 - Add New Item Option

Select Web Form and Check Select Master Page (see Figure 5)

Selecting Master Pages as a New Item

Figure 5 - Selecting Master Pages as a New Item

From the Select a Master Dialog, select the master page and click OK(see Figure 6)

Selecting Master Pages

Figure 6 - Selecting Master Pages

The Source View of Content Page

<% @ Page Language = "VB" MasterPageFile = "~/MasterPage.master" AutoEventWireup = "false" CodeFile = "Default.aspx.vb" Inherits = "_Default" title = "Untitled Page" %>
<
asp:Content ID = "Content1" ContentPlaceHolderID = "ContentPlaceHolder1" Runat = "Server"></asp:Content>

From the source of Master Page and the Content Page it is evident that whatever we add in the master page, are not present in the content page. Instead we have one directive

MasterPageFile = "~/MasterPage.master"

Which points to the master file and tells ASP.NET to use the template from that master page file

Even at design time we can see the gray-out area of the master page but only allowed to put content in the Content area only (see Figure 7).

A Content Page at Design Time.

Figure 7: A Content Page at Design Time.

At run-time ASP.NET engine combine these elements into a single page for the end-user (see Figure 8). This is possible now because .NET framework 2.0 now supports partial classes, which helps to take two classes and combine them into one Single class.

The Master and Content Page at Run time…

Figure 8: The Master and Content Page at Run time…

We can also programmatically include the master page in the following way:

VB CODE

Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Page.MasterPageFile = "~/MasterPage.master"

End
Sub

C# CODE

Protected void Page_PreInit(object sender, System.EventArgs e)
{
        Page.MasterPageFile = "~/MasterPage.master"
}

In many of the company websites, however, there are not just two layers: one master page and the content pages. Many divisions and groups exist within the organization that might want to use variations of master by, in effect, having a master within a master page. This is called nested master pages.

Main Master Page CODE

<%@ Master Language = "VB" CodeFile = "MasterPage.master.vb" 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>

Sub Master Page CODE

<%@ Master Language = "VB"  MasterPageFile = "~/MasterPage.master" %>
<!
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<
script runat = "server">
</
script>
<
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 = "ContentPlaceHolder2" runat = "server">
        </asp:contentplaceholder>
    </div>
    </form>

</
body>
</
html>

When you create applications that use a common header, footer, or navigation section on every pages of th eapplicatio, master pages are the solution. Master pages helps easily to implement and enable a developer to make changes to each and every page in the application by changing a single file.

This article described master pages in ASP.NET 2.0 and explained how to build a nd use master pages with a web application.


Page Options:
format for printing  Format for Printer
email article  Email Page
add to your favorites   Add to Favorites
How would you rate the quality of this content?
Poor - - Excellent
Comments?
Overall Rating:
Comments Left:
Left on 8/19/2008 7:46:00 AM by Anonymous
Comments: not bad
Left on 8/7/2008 8:13:30 AM by Anonymous
Comments: very useful
Left on 7/21/2008 1:36:50 AM by Anonymous
Comments: Brief but concise!
Left on 7/7/2008 6:07:50 AM by Anonymous
Comments: nice content....i have learnt a lot from this.
No ratings available.
Left on 3/19/2008 1:30:42 AM by Anonymous
Comments: superb!!!
Left on 1/3/2008 3:42:05 AM by Anonymous
Comments: But,give detailed description to how to add images to header and footer
Left on 11/24/2007 4:17:32 AM by Anonymous
Comments: Clearly it should be stated about how to set image as a page footer and many option should be taught in simple methods
Left on 11/19/2007 3:24:48 PM by Anonymous
Comments: Won't format for printer. Just gives a blank page.
Left on 11/11/2007 7:52:49 PM by Anonymous
Comments: Hello! Good Site! Thanks you! tvyfhsftjjrq
Left on 9/19/2007 2:08:35 PM by Anonymous
Comments: good
Left on 8/7/2007 1:58:56 PM by Anonymous
Comments: what if I have an image that I break into two. The first part can come in the master page, however the second part of the image has a text superimposed on it that is dependent on the content page. Can I create this page using master pages??
No ratings available.
Left on 3/23/2007 6:22:26 AM by Anonymous
Comments: dont dhow me again
Left on 9/11/2006 11:40:22 PM by Anonymous
Comments:
Left on 8/16/2006 2:28:33 PM by Anonymous
Comments: Exellent and thanks
Left on 7/28/2006 10:53:06 AM by Anonymous
Comments: does anyone know what markup these content / place holder controls get turned into at runtime? Please tell me thye dont get converted to tables :)

No ratings available.
Left on 7/27/2006 8:01:31 AM by Anonymous
Comments: It may be worth noting that master pages will work for creating common visual elements, but are not an alternative for deriving from the page class for shared logic. This is due to the way that the master page renders. It will in fact be rendered as a control on the ASPX at runtime. This means that the aspx init and load events will be hit BEFORE those on the master page. If you are relying on the master page to set up values that are then used in the content page this could cause some issues!
No ratings available.
Left on 7/26/2006 2:29:07 PM by Anonymous
Comments: uh...  is this supposed to be useful?  I can operate visual studio.
No ratings available.
Left on 7/2/2006 11:03:26 PM by Anonymous
Comments: Great stuff!
  

 Latest Articles
  

 Latest News
  

 

Spotlight
Syndication

 


 


Digg This
 


DotNetNuke Platinum Benefactor

  
 

 Terms Of Use | Privacy Statement
 Copyright 2008 - Santry Technology Solutions, Box 172, Girard, PA 16417, (814) 774-0970