Multiple Levels of Master Pages

来源:互联网 发布:韩端机器人编程难吗 编辑:程序博客网 时间:2024/05/20 05:56

   This feature provides a way to display standard content from several levels of hierarchy. For
example, a Content page named Publication.aspxcan establish its Master as Research.master,
which can in turn declare its Master as Corporate.master. The final rendering will be of Research
.aspxsurrounded by Research.master, and all of that surrounded by Corporate.master. One prob-
lem is that the space remaining for content is reduced at each level. VWD does not contain automatic
tools to create multiple levels of Masters. In fact, if you have multiple levels, you can only open those
pages in Source View.
 

    To create pages with multiple levels of Masters you must include in the middle-level page tags that both
indicate its Master page (up a level) and its content placeholders (for the lower level). Recall that a
Master page must have <%@Master...%>on the first line, and that a lower-level or Content page must
have <%@PageMasterPageFile=%>on its first line. In the case of a middle page that is both a Content
and Master page, the tag must start with <%@Master...and also contain ...MasterPageFile=%>.
Also recall that a Master page contains an <asp:ContentPlaceHolder>tag whereas the Content page
has an <asp:content>tag. In the case of a middle layer, there must be an <asp:content>holding the
ID of the Master’s <ContentPlaceHolder>tag. Then within that tag there is an <asp:
ContentPlaceHolder>tag that will be used by the next level down.
The following example illustrates a Corporate Master page, then a Research department Master page,
and finally a Publication.aspxpage that holds the content. The Corporate page is shown in the fol-
lowing code. Note that its content placeholder is defined in the shaded lines:

<%@ Master Language=”C#” %>
<!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 runat=”server”>
<title>CorporateMaster</title>
</head>
<body>
Corporation Name
<form id=”form1” runat=”server”>
<div>
<asp:contentplaceholder 
id
=”ContentPlaceHolderCorporate” 
runat
=”server”>
</asp:contentplaceholder>
</div>
</form>
</body>
</html>

   The Research department Master page is illustrated by the following code. This page is the most com-
plex, because it is a Content page for the Corporate page, and a Master page for the Publication page.
Notice the use of Masterand MasterPageFile=on the first line that establishes this as content for the
Corporate Master page. Then observe that the <asp:ContentPlaceHolder>will house other pages as
content (in this case the Publication page). The content placeholder must sit wholly within the

<asp:content>tags:
<%@ Master MasterPageFile=~/Corporate.master”  Language=”C#” %>
<asp:Content runat=”server” 
ContentPlaceHolderID
=”ContentPlaceHolderCorporate”>
Research Department
<asp:contentplaceholder 
id
=”ContentPlaceHolderResearch” 
runat
=”server”>
</asp:contentplaceholder>
</asp:Content>

    Code for the Publication.aspxpage (designed with content only) is shown in the following code.
Here you only need to designate a Master page. This page, which sits at the lowest level, is not a Master
page:

<%@ Page Language=”C#” MasterPageFile=~/Research.master” Title=”Untitled Page” %>
<asp:Content ID=”Content1” 
ContentPlaceHolderID
=”ContentPlaceHolderResearch” 
Runat
=”Server”>
Publication text 
</asp:Content>