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>  

適用於

另請參閱