How to Use Master Pages (Conclusion)
(Page 1 of 6 )
Now that you have a handle on creating master pages, you might want to develop some content for that page. This article, the second of two parts, finishes our discussion of topics related to master pages. It is excerpted from chapter three of the book
Murach’s ASP.NET 2.0 Upgrader’s Guide: VB Edition, written by Anne Boehm and Joel Murach (Murach, 2005; ISBN: 1-890774-36-7).
How to create and develop content pages
Once you create a master page, you can create and develop the content pages for the master page. The topics that follow show how.
How to create a content page
Figure 3-6 shows how to create a content page. In short, you use the same procedure to create a content page that you use to create a regular page, but you check the Select a Master Page check box. Then, you can choose the master page you want to use for the content page from the Select a Master Page dialog box that’s displayed.
Alternatively, you can select the master page you want to use in the Solution Explorer. Then, choose the Website�Add Content Page command. This creates a content page for the selected master page. Note that when you use this technique, the content page is automatically named Default.
The code example in this figure shows the code that’s generated when you create a new content page for the master page shown in figure 3-4. This code is quite different from the code that’s generated when you create a regular ASP.NET page. Although the Page directive includes the same information as a regular ASP.NET page, it also includes a MasterPageFile attribute that specifies the master page you selected. And the rest of the content page is completely different from a normal ASP.NET page.
Before I describe the other differences, you should know that the title you specify in the Title attribute of the Page directive of a content page overrides any title you specify in the master page. That way, you can display a different title for each content page. If you want to use the same title for each content page, however, you can specify the title in the master page and then delete the Title attribute from the content pages.
Unlike normal ASP.NET pages, content pages don’t include a Doctype directive or any structural HTML elements such as html, head, body, or form. That’s because those elements are provided by the master page. Instead, the content page includes an ASP.NET Content element that indicates which content placeholder the page should be displayed in. Then, you place the content that you want to display on the page between the start and end tags of this element.
This figure also includes a procedure for converting a regular page to a content page. You’ll need to follow this procedure if you start a web site without using master pages, and later decide to use master pages. Unfortunately, though, Visual Studio doesn’t provide a way to automatically do this. As a result, you’ll have to manually edit each of the pages to add the MasterPageFile attribute to the Page directive, remove the Doctype directive and structural HTML elements (html, head, body, and form), and add a Content element.
The aspx code for a new page that uses the master page in figure 3-4
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Order.aspx.vb" Inherits="Order" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="server">
</asp:Content>
How to create a new content page (figure 3-6)
- One way is to choose the Website->Add New Item command. Then, select Web Form from the list of templates, enter the name for the form, check the Select a Master Page check box, and click Add. When the Select a Master Page dialog box appears, select the master page you want and click OK.
- Another way is to select the master page in the Solution Explorer, then choose the Website->Add Content Page command.
How to convert a regular ASP.NET page to a content page
- First, add a MasterPageFile attribute to the Page directive that specifies the URL of the master page. Next, replace the Div element that contains the actual content of the page with a Content element as shown above. Then, delete everything that’s outside this Content element except for the Page directive.
Two other ways to specify the master page In the web.config file
<system.web >
.
<pages masterPageFile="MasterPage.master" />
.
</system.web>
In the Page_PreInit procedure
Protected Sub Page_PreInit(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.PreInit
MasterPageFile = "MasterPage.master"
End Sub
Description
- The Page directive in the aspx code for a content page includes a MasterPageFile attribute that specifies the name of the master page.
- The aspx code for a content page includes a Content element that indicates the ID of the content placeholder where the content for the page should be displayed. Any content you create for the page should be placed between the start and end tags for this element.
- You can also specify the master page in the web.config file or in the Page_PreInit procedure. However, the Web Forms Designer doesn’t support either of these techniques, so you won’t be able to view the content page in Design view.
Because this conversion procedure is error prone, it pays to use master pages for all but the simplest of applications, even if each master page contains only a content placeholder. Then, when you’re ready to provide a consistent look to the pages within the application, you can enhance the master pages.
This figure also shows two other ways to specify which master page is used with a content page. First, you can add a <pages> element to the web.config file with a MasterPageFile attribute that specifies the master page to be used with all pages that don’t specify a master file. Second, you can specify the master page at runtime by setting the MasterPageFile attribute of the page in the Page_PreInit procedure. Note, however, that the Web Forms Designer doesn’t support either of these techniques. If you use them, then, you won’t be able to view or edit your content pages in Design view.
Next: How to add content to a page >>
More ASP.NET Articles
More By Murach Publishing
|
This article is excerpted from chapter three of the book Murach’s ASP.NET 2.0 Upgrader’s Guide: VB Edition, written by Anne Boehm and Joel Murach (Murach, 2005; ISBN: 1-890774-36-7). Check it out today at your favorite bookstore. Buy this book now.
|
|