共用方式為


MasterPage.Master 屬性

定義

在巢狀主頁情境下取得目前主版的父主頁。

public:
 property System::Web::UI::MasterPage ^ Master { System::Web::UI::MasterPage ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.UI.MasterPage Master { get; }
[<System.ComponentModel.Browsable(false)>]
member this.Master : System.Web.UI.MasterPage
Public ReadOnly Property Master As MasterPage

屬性值

作為目前主頁面父頁的母頁;否則, null若目前母頁沒有父頁,則 。

屬性

範例

本節包含三個程式碼範例。 第一個程式碼範例示範如何建立巢狀母版頁面. 。第二個範例則示範如何參考第一個範例中建立的母版頁。 第三個程式碼範例示範如何使用內容頁來參考第二個範例中建立的母版頁面。

以下程式碼範例示範如何建立巢狀母版頁面,並表示名為 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 了前述程式碼範例中所建立的 that。

<%@ 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>

備註

Master 屬性為唯讀,因為執行時無法建立主頁階層結構。

適用於

另請參閱