嵌套母版页在asp.net

本文关键字:net asp 母版页 嵌套 | 更新日期: 2023-09-27 18:17:57

在嵌套的母版页中,子母版页定义了@ Master声明中设置的MasterPageFile属性。这个属性指向父母版页。

嵌套母版页在asp.net

是的,这是真的。

母版页:

<% @ 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>Untitled Page</title>
</head>
<body>
<form id="Form1" runat="server">
<div>
<h1>Parent Master</h1>
<p style="font:color=red">This is parent master content.</p>
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
</div>
</form>
</body>
</html>

子母版页:

<%@ Master Language="C#" MasterPageFile="~/Parent.master"%> 
<asp:Content id="Content1" 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 child master content.</p>
        <asp:ContentPlaceHolder ID="ChildContent1" runat="server" />
      </asp:panel>
      <asp:panel runat="server" id="panel2" backcolor="pink">
         <p>This is child master content.</p>
         <asp:ContentPlaceHolder ID="ChildContent2" runat="server" />
      </asp:panel>
      <br />
   </asp:panel>
</asp:Content>

这是一个引用子母版页的子文件:

<%@ Page Language="C#" MasterPageFile="~/Child.master"%>
<asp:Content id="Content1" ContentPlaceholderID="ChildContent1" runat="server">
   <asp:Label runat="server" id="Label1" 
        text="Child label1" font-bold="true" />
   <br />
</asp:Content>
<asp:Content id="Content2" ContentPlaceholderID="ChildContent2" runat="server">
   <asp:Label runat="server" id="Label2" 
        text="Child label2" font-bold="true"/>
</asp:Content>