ASP.NET 2.0中嵌套母版页的应用


Posted on September 7, 2007


之前写过母版页的使用,但那只是在一个页面使用了母版页。但实际中需要使用多个母版嵌套使用的情况,也就是父母版页中还要包含子母版页。
default.aspx
<%@ Page Language="VB" MasterPageFile="Child.Master"%>
<asp:Content id="Content1" ContentPlaceholderID="Content1" runat="server">
   <asp:Label runat="server" id="Label1"
        text="Child label1" font-bold="true" />
   <br>
</asp:Content>
<asp:Content id="Content2" ContentPlaceholderID="Content2" runat=server>
   <asp:Label runat="server" id="Label2"
        text="Child label2" font-bold=true/>
</asp:Content>
 
Child.Master
<%@ master Language="VB" MasterPageFile="Parent.master"%>
<asp:Content id="Content11" ContentPlaceholderID="MainContent" runat="server">
   <asp:panel runat="server" id="panelMain" backcolor="lightyellow">
   <h2>Child master</h2>
      <asp:panel runat="server" id="panel1" backcolor="lightblue">
        <p>This is childmaster content.</p>
        <asp:ContentPlaceHolder ID="Content1" runat="server" />
      </asp:panel>
      <asp:panel runat="server" id="panel2" backcolor="pink">
         <p>This is childmaster content.</p>
         <asp:ContentPlaceHolder ID="Content2" runat="server" />
      </asp:panel>
   </asp:panel>
</asp:Content>
 
Parent.master
<% @ Master Language="VB" %>
<!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" >
<body>
<form id="Form1" runat="server">
<div>
<h1>Parent Master</h1>
<p>
<font color="red">This is parent master content.</font>
</P>
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>
</form>
</body>
</html>
将这三个文件放到一个目录下,就可以看到效果了。

其中要理解<asp:ContentPlaceHolder>和<asp:Content>的关系,<asp:ContentPlaceHolder>的ID要和<asp:Content>的ContentPlaceholderID相同。理解了就可以轻松运用母版页了。


标签:N/A

 

在线学习答案查询入口
微信扫一扫
微信扫码联系