An In-Depth Coverage of ASP.NET 2.0's Master Pages: Part 1 of 3
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
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:
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:
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:
- ASP.NET fetches the page.
- ASP.NET determines whether the content page references a master page.
- If so, ASP.NET fetches the master page associated with the content page.
- ASP.NET merges the master page into the content page and treats it as a control in the content page.
- 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
Downloads
More for Developers
Top Authors
- Voted: 13 times.
- Voted: 11 times.
- Voted: 11 times.
- Voted: 8 times.
- Voted: 8 times.
- Paul Kimmel 214 articles
- Zafir Anjum 120 articles
- 15Seconds.com 99 articles
- Tom Archer - MSFT 83 articles
- Jeffrey Juday 82 articles


All