Flylib.com

Books Software

 
 
 

Chapter 1. Master Pages


Chapter 1. Master Pages



1.0 Introduction

Most applications use a common HTML design for the majority of their pages, and in the past, developers have conceived many techniques to reduce the replication of the identical HTML in the application pages. However, none of those techniques met the primary goals of providing the HTML in one place or made it easy for non-technical personnel to edit the HTML.

ASP.NET 2.0 provides a new approach with master pages that comes close to meeting these primary goals. Master pages contain all of the common HTML, server controls, and code that would normally be replicated in some fashion in the application pages. You place one or more content placeholders in the master to reserve the space for page-specific content. Content pages provide the content specific to the page and need only reference the desired master page. When a user requests a content page, ASP.NET merges the output of the master page with the output of the content page, resulting in a page that combines the master page layout with the output of the content page.

This chapter provides four recipes for master pages. These include a quick master/content recipe to familiarize you with the technique. Next, we show how to extend a master page's content to include content for other application pages, which you might want to do, for example, when you want your login page to have one appearance and the pages that follow it to build on that appearance. The third recipe describes how to set the master page for pages within a folder structure without having to set the master page for each content page explicitly. This is handy when you want to use the same master page for the majority of your content pages but change to another master page as needed without having to edit all of the pages in the application. The last recipe describes how to set the master page programmatically, which provides the ability to change how the application appears at runtime; this approach is useful when you want to change the appearance of the rendered pages based on the season or client branding of some sort .

In addition to showing you some useful techniques for using master pages, the recipes found in this chapter serve to provide the consistent appearance for all the recipes in the book. In particular, you will see that we use Recipe 1.1's ASPNetCookbookVB.master master page in all the recipes in the book, saving us from having to repeat considerable HTML formatting in all the recipes, something that we were unable to do in the previous edition of this book, which focused on ASP.NET 1.x.



Recipe 1.2. 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.

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.

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>