语言

MasterPage.MasterPageFile 属性

定义

获取或设置包含当前内容的母版页的名称。

public:
 property System::String ^ MasterPageFile { System::String ^ get(); void set(System::String ^ value); };
public string MasterPageFile { get; set; }
member this.MasterPageFile : string with get, set
Public Property MasterPageFile As String

属性值

母版页的名称,该母版页是当前母版页的父级;否则, null如果当前母版页没有父级,则为 。

例外

该 MasterPageFile 属性只能在事件中或事件之前 PreInit 设置。

示例

本部分包含三个代码示例。 第一个代码示例演示如何创建嵌套母版页. 。第二个代码示例演示如何引用在第一个代码示例中创建的母版页。 第三个代码示例演示如何使用内容页引用第二个代码示例中创建的母版页。

下面的代码示例演示如何创建嵌套母版页,并表示名为 ParentMasterPage_1 的父母版页。

<%@ Master Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html >
<head runat="server">
    <title>Nested Master Page Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <h1>This is content in the parent master page.</h1>
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>
<%@ Master Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html >
<head runat="server">
    <title>Nested Master Page Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <h1>This is content in the parent master page.</h1>
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>

下面的代码示例演示如何使用一个名为引用 ChildMasterPage_1 前面代码示例中创建的母版页的嵌套母版页。

<%@ Master Language="C#" MasterPageFile="~/ParentMasterPage_1cs.master" %>

<asp:Content Runat="Server" ContentPlaceHolderID="ContentPlaceHolder1">
    <h2>This is the content of a nested Master Page.</h2>
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder2" runat="server">
        </asp:contentplaceholder>
    </div>
</asp:Content>
<%@ Master Language="VB" MasterPageFile="~/ParentMasterPage_1vb.master" %>

<asp:Content Runat="Server" ContentPlaceHolderID="ContentPlaceHolder1">
    <h2>This is the content of a nested Master Page.</h2>
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder2" runat="server">
        </asp:contentplaceholder>
    </div>
</asp:Content>

下面的代码示例演示如何使用一个名为 ContentPage 引用 ChildMasterPage_1 前面代码示例中创建的内容页。

<%@ Page Language="C#" MasterPageFile="~/ChildMasterPage_1cs.master" Title="Untitled Page"%>

<asp:content runat="server" contentplaceholderid="ContentPlaceHolder2">
This is the content of a Content control.
</asp:content>
<%@ Page Language="VB" MasterPageFile="~/ChildMasterPage_1vb.master" Title="Untitled Page"%>

<asp:content runat="server" contentplaceholderid="ContentPlaceHolder2">
This is the content of a Content control.
</asp:content>

适用于

另请参阅