CodeGuru
Earthweb Search
Login Forums Wireless Jars Gamelan Developer.com
CodeGuru Navigation
RSS Feeds

RSSAll

RSSVC++/C++

RSS.NET/C#

RSSVB

See more EarthWeb Network feeds

follow us on Twitter

Member Sign In
User ID:
Password:
Remember Me:
Forgot Password?
Not a member?
Click here for more information and to register.

Become a Marketplace Partner

jobs.internet.com

internet.commerce
Partners & Affiliates
















Home >> .NET / C# >> .NET >> General ASP.NET >> Web Forms


An In-Depth Coverage of ASP.NET 2.0's Master Pages: Part 1 of 3
Rating:

Manoj Agarwal (view profile)
December 30, 2005

Environment:  ASP.NET 2.0

Go to page: Prev  1  2  

Content Pages

Once you have moved the common UI elements and code to the master page, only the page specific part will remain in the ASPX pages. This page-specific content is placed within the content control. In an ASPX page that has a master page, no content can be outside the content control; otherwise, ASP.NET would not know how to merge this page with the master page. For the ASPX page, you specify which master page to use in the page's @ Page directive by including a MasterPageFile attribute that points to the master page to be used. The content pages for your Home and About Us pages are:

Home.aspx

<%@ Page MasterPageFile="~/SiteMaster.master"
    CodeFile="Home.aspx.cs"
    Inherits="Home"
    Title="Home Page" %>
<asp:Content ID="Content1"
     ContentPlaceHolderID="myHolder"
     runat="Server">
   Welcome to Falconsoft Systems - a company formed by people
   having worked in the industry for a good many years. <p />
</asp:Content>

AboutUs.aspx

<%@ Page MasterPageFile="~/SiteMaster.master"
         CodeFile="AboutUs.aspx.cs"
         Inherits="AboutUs"
         Title="About Us Page" %>
<asp:Content ID="Content1"
     ContentPlaceHolderID="myHolder"
     Runat="Server">
   About Us - A group of IT professionals who shared a common
   vision and outlook to software design and development came
   together to form this company. <p />
</asp:Content>

AboutUs.aspx.cs

using System;
using System.Web;

public partial class AboutUs : System.Web.UI.Page
{
}

Default Content


(continued)




Along with layout and code that applies to all pages, the master page can also supply default content, which can be overridden by content pages or displayed if not overridden. This is achieved by simply inserting the content within the ContentPlaceHolder element. For example, your MySite.master page could have the following default content:

<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
   <h2>Welcome</h2>
   Welcome to my site, where you'll find lots of interesting stuff.
</asp:ContentPlaceHolder>

Creating Master Pages through Visual Studio

In Visual Studio 2005, the way one creates an ASP.NET application has been changed a little. It can be created from File->New->Web Site. Another change made is that it's not mandatory to have IIS on the ASP.NET development machine because Visual Studio installs an ASP.NET Development Server in which the application can run.

After creating a new Web site, one can add a master page from Website->Add New Item. The Add New Item screen is shown below:


(
Full Size Image)

Figure 4: The Add New Item screen to add a master page to the Web project

After the master page has been added, you can add a Web form that uses the master page. To do this, check the Select master page option in the Add New Item screen, as shown below:


(Full Size Image)

Figure 5: Check the Select master page option to add a Web form that uses a master page

Runtime Behavior of Master Pages

The client never requests the master page directly and the master page is not known to the client. The client requests the content page (ASPX pages); if there is a master page associated with it, the master page is merged into the content page and treated as a control in the content page. ASP.NET processes a page request in the following sequence:

  1. ASP.NET fetches the page.
  2. ASP.NET determines whether the content page references a master page.
  3. If so, ASP.NET fetches the master page associated with the content page.
  4. ASP.NET merges the master page into the content page and treats it as a control in the content page.
  5. ASP.NET renders results to the browser.

Some Useful Links

1. This book has a great chapter on master pages. (You can download master page chapter for free!)
A First Look at ASP.NET v 2.0

2. The master pages section on ASP.NET Web:
Visual Web Developer 2005 Express Edition Guided Tour

About the Author
Manoj Agarwal has over a decade of industry experience on a wide spectrum of technologies. He currently runs a software company called Falconsoft Systems which specializes in consulting, application development and training mainly in Microsoft .NET. Manoj was among the top-10 winners at the nationwide Microsoft Technology Adopter Challenge.

Go to page: Prev  1  2  

Downloads

  • MasterPage_SimpleSample.zip - Sample website which uses master pages

    Tools:
    Add www.codeguru.com to your favorites
    Add www.codeguru.com to your browser search box
    IE 7 | Firefox 2.0 | Firefox 1.5.x
    Receive news via our XML/RSS feed







  • RATE THIS ARTICLE:   Excellent  Very Good  Average  Below Average  Poor  

    (You must be signed in to rank an article. Not a member? Click here to register)

    Latest Comments:
    No Comments Posted.
    Add a Comment:
    Title:
    Comment:
    Pre-Formatted: Check this if you want the text to display with the formatting as typed (good for source code)



    (You must be signed in to comment on an article. Not a member? Click here to register)

    internet.commediabistro.comJusttechjobs.comGraphics.com

    Search:

    WebMediaBrands Corporate Info

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs