Recipe 1.4.
Changing Which Master Page Is Used Without Modifying All Affected
Application Pages
Problem
You want to use the same master page for all
pages in a section of your application, but you want the ability to
change which master page is used without having to modify all of
the affected pages, something you cannot do with the more
traditional master/content page approaches described in the
previous recipes in this chapter.
Solution
Implicitly assign a master page to a content
page as
follows
:
-
Create a new folder within your application.
-
Create the master page your application will be
using.
-
Place all pages that will be using the master
page in the folder.
-
Create a
web.config
file that contains a
<pages>
element with the
masterPageFile
attribute set to the
name
of the
.master
file and place it in the folder.
Example 1-5 shows the
web.config
file used to set the master page
implicitly, and Examples 1-6 and 1-7 show the
.master
and
.aspx
files for our example.
Discussion
In some applications, it is desirable to use the
same master page for a large number of content pages and, at the
same time, to be able to change to another master page without
having to edit all of the pages in the application. ASP.NET 2.0
provides the ability to assign the master page implicitly to
content pages by using the new
masterPageFile
attribute of
the
<pages>
element in the
web.config
file. Setting the
masterPageFile
attribute to the name of a
.master
file
assigns
the master page to all
content pages in the folder where the
web.config
file is located. This assigns the
master page to all content pages in all
subfolders
unless another
web.config
file in a subfolder
overrides
the setting.
ASP.NET provides a lot of flexibility when using
this approach. Any content page can still explicitly set the
MasterPageFile
attribute in the
@ Page
directive,
as described in Recipe 1.1, which will override the setting in the
web.config
file. This is
convenient
when you have a small number of pages that need to be
handled differently; however, this can become confusing when the
assignment of a master page is changed in the
web.config
file and the developer is expecting
it to affect all pages.
|
Implicitly assigning a master page using this
approach will not work for nested master pages. If a nested master
page is included without explicitly setting the
MasterPageFile
attribute of the
@ Master
directive, you will get an error indicating that content controls
are only allowed in pages that reference a master page.
|
|
In our example, we have created a new folder and
placed in it the
web.config
file
shown in Example 1-5 along with the
.master
and
.aspx
files shown in Example 1-6 and Example
1-7. The primary difference between this example and the example
shown in Recipe 1.1 is the removal of the
MasterPageFile
attribute from the
@ Page
directive of the content page
and the presence of the
web.config
file.
|
Using this approach can cause problems
referencing images and stylesheets. This is particularly an issue
if a master page uses images, is located in the root directory, and
is then used in a subfolder. The page is rendered using the
path
to
the subfolder resulting in the URL for images pointing to the
subfolder. If the images exist only in the root folder, the images
referenced by the master page will not be displayed.
|
|
See Also
Recipes 1.1 and 1.2
Example 1-5.
web.config file for implicitly assigning a master page to a content
page
<?xml version="1.0" ?>
<configuration>
<system.web>
<pages masterPageFile="~/CH01AttachMaster/ASPNetCookbookVB.master" />
</system.web>
</configuration>
|
Example 1-6.
Implicitly assigning a master page to a 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" />
</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-7.
Implicitly assigning a master page to a content page (.aspx)
<%@ Page Language="VB"
AutoEventWireup="false"
title="Attaching Master Pages Using Web.Config" %>
<asp:Content ID="Content1" ContentPlaceHolderID="PageBody" Runat="Server">
<div align="center" class="pageHeading">
Attaching Master Pages Using Web.Config (VB)
</div>
<br />
<p align="center">The content for your pages is placed here.</p>
</asp:Content>
|
|