Sections
     
     
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 3/12/2010 12:34:18 AM by Anonymous
Comments: you have to given crtain example about it
Left on 2/20/2010 7:58:14 AM by Anonymous
Comments: more clarity needed
Left on 2/10/2010 6:34:23 AM by Anonymous
Comments: its verry good content
Left on 10/12/2009 1:23:50 PM by Anonymous
Comments: following this can create master page. Then other webforms are added to master. Problem is here in homewebform already include mastercode but don't know to develop for a body part of homewebform.
No ratings available.
Left on 10/6/2009 1:46:56 PM by Anonymous
Comments: how to add a new master page to other webform..because forget to click master page during create the newpage

No ratings available.
Left on 9/2/2009 1:24:07 AM by Anonymous
Comments: First u learn then teach .
Left on 8/23/2009 11:45:21 PM by Anonymous
Comments: good article. thanks a lot
Left on 8/11/2009 9:41:23 AM by Anonymous
Comments: d
Left on 6/4/2009 5:02:29 AM by Anonymous
Comments: nice article.
Left on 6/2/2009 12:46:26 PM by Anonymous
Comments: Thank You very much.
Left on 4/23/2009 2:31:27 PM by Anonymous
Comments: Said nothing
Left on 3/28/2009 2:29:55 AM by Anonymous
Comments: Hello, it is nice & it give the basic ideas of master pages.
No ratings available.
Left on 2/26/2009 2:30:11 AM by Anonymous
Comments: its good to have basic idea on Master Pages
Left on 1/20/2009 12:46:18 AM by Anonymous
Comments: Hi..! it's nice artical but it is more helpful if u give small example along with it .
No ratings available.
Left on 1/4/2009 11:25:38 AM by Anonymous
Comments: pls provide asp.net c# code.......
No ratings available.
Left on 12/22/2008 7:02:24 AM by Anonymous
Comments: it is good but not to good if u want to know tha coding about c# asp.net this is not very important
No ratings available.
Left on 12/11/2008 12:27:30 AM by Anonymous
Comments: Good work... Keep writing..
Left on 12/4/2008 10:06:23 PM by Anonymous
Comments: NICE INFO>>GREAT WORK DUDE
Left on 11/17/2008 11:14:59 AM by Anonymous
Comments: very nice thank you very much sir its ammazing.......
Left on 10/29/2008 6:22:25 PM by Anonymous
Comments: nemir test
No ratings available.
Left on 9/4/2008 12:38:46 AM by Anonymous
Comments: its good . i came to know(i learn from this article) what is Master page and Content page form this Article
Left on 9/3/2008 12:30:43 AM by Anonymous
Comments: very helpful
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!
     
     

 

     
     

 


 


Digg This
 


DotNetNuke Platinum Benefactor

     
     

Other family network sites: santry.com - katieandkarleigh.com

Powered by 

 

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