Recipe 1.2. Generating a Quick Master/Content Page Arrangement
You want to generate a master/content page arrangement quickly to explore the approach used for master pages.
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:
In the .master file of the master page:
In the .aspx file:
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
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 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 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 ContentPlaceHolder 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.
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.
Example 1-1. Quick master/content page (.master)
Example 1-2. Quick master/content page (.aspx)