Changing Which Master Page Is Used Without Modifying All Affected
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.
Implicitly assign a master page to a content
Create a new folder within your application.
Create the master page your application will be
Place all pages that will be using the master
page in the folder.
file that contains a
element with the
attribute set to the
file and place it in the folder.
Example 1-5 shows the
file used to set the master page
implicitly, and Examples 1-6 and 1-7 show the
files for our example.
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
element in the
file. Setting the
attribute to the name of a
the master page to all
content pages in the folder where the
file is located. This assigns the
master page to all content pages in all
file in a subfolder
ASP.NET provides a lot of flexibility when using
this approach. Any content page can still explicitly set the
attribute in the
as described in Recipe 1.1, which will override the setting in the
file. This is
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
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
attribute of the
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
shown in Example 1-5 along with the
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
attribute from the
directive of the content page
and the presence of the
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
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.
Recipes 1.1 and 1.2
web.config file for implicitly assigning a master page to a content
<?xml version="1.0" ?>
<pages masterPageFile="~/CH01AttachMaster/ASPNetCookbookVB.master" />
Implicitly assigning a master page to a content page (.master)
<%@ Master Language="VB"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
<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" />
<form id="form1" runat="server">
<div align="center" class="header">
<img src="../images/ASPNETCookbookHeading_blue.gif" />
<asp:ContentPlaceHolder ID="PageBody" Runat="server" >
<h4>Default Content Displayed When No Content Is Provided In
Implicitly assigning a master page to a content page (.aspx)
<%@ Page Language="VB"
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)
<p align="center">The content for your pages is placed here.</p>