1.1. Generating a Quick Master/Content Page Arrangement

Problem

You want to generate a master/content page arrangement quickly to explore the approach used for master pages.

Solution

Create a .master file that contains the common HTML for your pages and then create an .aspx file that contains the page-specific content.

Create a new master page by following these steps:

  1. Select your web site in the Solution Explorer.

  2. Right-click and select Add New Item from the context menu.

  3. Select Master Page from the Add New Item dialog.

In the .master file of the master page:

  1. Add the HTML that is common for your application pages.

  2. Add an asp:ContentPlaceHolder control for each portion of the page that will have page-specific content.

  3. Set the ID attribute to a unique identifier.

In the .aspx file:

  1. Set the MasterPageFile attribute of the @Page directive to the name of the .master file.

  2. Add an asp:Content control for each asp:ContentPlaceHolder control in the .master file.

  3. Set the ContentPlaceHolderID attribute of the asp:Content to the ID of the corresponding asp:ContentPlaceHolder in the .master file.

  4. Add the page-specific content to the asp:Content controls.

Figure 1-1 shows the output of the master/content page in our example. Example 1-1 shows the .master file, and Example 1-2 shows the .aspx file of our example.

Quick master page example output

Figure 1-1. Quick master page example output

Discussion

Implementing a basic master/content page arrangement requires no coding. The .master file consists of an <%@ Master…%> directive at the top of the page instead of an <%@ Page…%> directive and the common HTML (and server controls if required). In addition, it contains one or more asp:ContentPlaceHolder controls that reserve the space for the page-specific content. At a minimum, the ID and Runat attributes must be set, with the ID attribute being set to a unique identifier.

	<asp:ContentPlaceHolder ID="PageBody" Runat="server" />

Optionally, the asp:ContentPlaceHolder control can contain default content that is displayed if a content page does not provide any content for the placeholder.

	<asp:ContentPlaceHolder ID="PageBody" Runat="server" >
		 <div align="center">
              <br />
			  <br />
			  <h4>Default Content Displayed When No Content Is Provided In
                        Content Pages</h4>
         </div>
    </asp:ContentPlaceHolder>

At a minimum, the .aspx file for the content page contains the @ Page directive with the MasterPageFile attribute set to the name of the master page that will provide the template for the content page and an asp:Content control that contains the page-specific content.

	<%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master"
		   AutoEventWireup="false"
		   Title="CH01 Quick Master/Content Example" %>
	<asp:Content ID="Content1" ContentPlaceHolderID="PageBody" Runat="Server">
           …
	</asp:Content>

The ContentPlaceHolderID attribute of the asp:Content control must be set to the ID of an asp:ContentPlaceHolder control in the master page. Setting the ID in this manner identifies which placeholder the content is placed in. Connecting the asp:Content control to the asp:ContentPlaceHolder control in this manner provides the ability to have multiple placeholders in a master page.

Tip

The .aspx files for pages that are linked to master pages do not contain the HTML, head, or body elements. In addition, they are not allowed to have any content outside of the asp:Content controls in the page. Placing any content outside of the asp:Content control results in a parse error when the page is compiled.

In a master/content page arrangement, the page title element is located in the .master file. To provide the ability to set the title for content pages, the @ Page directive of the .aspx file has a Title attribute. Setting the value of the Title attribute in the @ Page directive results in the title of the rendered page being set to the title specified.

See Also

Recipe 1.2

Example 1-1. Quick master/content page (.master)

<%@ Master Language="VB" 
			  AutoEventWireup="false" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
             "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head id="Head1" runat="server">
     <title>ASP.NET Cookbook 2nd Edition</title>
	 <link rel="Stylesheet" href="css/ASPNetCookbook.css" alt="ASPNETCookbook"/> 
</head> 
<body>
     <form id="form1" runat="server">
          <div align="center" class="header">
               <img src="images/ASPNETCookbookHeading_blue.gif" />
          </div>
		  <div>
               <asp:ContentPlaceHolder ID="PageBody" Runat="server" >                    
<div align="center">
                         <br />
						 <br />
						 <h4>Default Content Displayed When No Content Is Provided
                                   In Content Pages</h4>
                    </div>
               </asp:ContentPlaceHolder>
          </div>
     </form> 
</body> 
</html>

Example 1-2. Quick master/content page (.aspx)

<%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master" 
       AutoEventWireup="false" 
	   Title="CH01 Quick Master/Content Example" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PageBody" Runat="Server"> 
     <div align="center" class="pageHeading">
          Quick Master and Content Page (VB)
     </div>
	 <br />
	 <br />
	 <p align="center">The content for your pages is placed here.</p>
</asp:Content>

Get ASP.NET 2.0 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.