如何使用 MasterPage
1. 建立 MasterPage,尾碼名 .master, 如 x.master.
其中用 <asp:ContentPlaceHolder /> 定義空位。如:
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" Runat="Server">
</asp:ContentPlaceHolder>
2. 建立內容頁面。
在 NewItem 對話方塊裡選擇 "select master page", 選擇上一步建立的 MasterPage.
產生的代碼裡, MasterPageFile 屬性指定了 MasterPage 的位置:
<%@ Page Language="VB" MasterPageFile="~/x.master" Title="無標題頁面" %>
頁面裡用 <asp:Content /> 來新增內容到對應的空位:
<asp:Content ID="Content1" ContentPlaceHolderId="ContentPlaceHolder1" Runat="Server">
內容
</asp:Content/>
內容頁面沒有 <form id="form1" runat="server">
3. 利用 MasterPage 可以使用多種語言來編寫一個頁面的各個部分。
4. 除了在 <%@ Page %> 裡面指定 MasterPage, 也可以在 web.config 指定:
<configuration>
<system.web>
<pages masterPageFile="~/x.master" />
</system.web>
</configuration>
這樣定義後,如果建立 Page 時選擇了 master page, 則在 <%@ Page %> 裡面不需要指定即可使用該 MasterPage.
其他頁面要使用不同的 MasterPage 的話,只要用第一種方法在 Page directive 裡面明確的覆蓋 web.config 裡的設定即可。
可以僅對一組 pages 指定 MasterPage. 下例利用 web.config 的 location 元素,設定了 Admin 目錄下的頁面採用的不同的 MasterPage.
<configuration>
<location path="Admin">
<system.web>
<pages masterPageFile="~/y.master" />
</system.web>
</location>
</configuration>
5. 在內容頁面如何設定 Page 的 Title ?
預設情況下,Title 在 MasterPage 中指定後,其他具體頁面就都使用這個 Title.
在具體頁面,可以有兩個辦法修改 Title:
a. <%@ Page Title="test" %>
b. 代碼中:
protected void Page_LoadComplete(object sender, EventArgs e)
{
Master.Page.Title = "Hello";
}
6. 訪問 MasterPage 中的屬性和控制項。
用 Master 屬性來訪問。
a. 假設 MasterPage 中有一個 Label1, 那麼在內容頁面可以這樣:
protected void Page_LoadComplete(object sender, EventArgs e)
{
string text = (Master.FindControl("Label1") as Label).Text;
}
頁面載入的次序:
要擷取在 MasterPage 的 Page_Load 裡面設定的值,必須在內容頁面的 Page_LoadComplete 中來寫。
前面提到的 FindControl() 方法來尋找 MasterPage 中的控制項,是一種後期綁定的做法,一般是不安全的。因為這取決於 MasterPage 中是否存在這個 tag,如果被刪除了,則會導致錯誤。
比較好的做法是,在 MasterPage 中用屬性封裝對他的控制項的訪問;如果用 FindControl(), 則總是檢查其結果是否為 null.
7. 指定 MasterPage 中的預設內容
直接在 <asp:ControlPlaceHolder /> 標籤之間指定即可。
如果子頁面不重新指定,則會採用該預設內容。
8. 編程的方式指定 Master Page
protected void Page_PreInit(object sender, EventArgs e)
{
Page.MasterPageFile = "~/x.master";
}
9. 嵌套的 Master Page
Master Page 可以繼承自更高層次的 Master Page. 但是在 VS2005 中建立這種子 Master Page 的時候,不會有預設的支援。
假設有了一個 A.master,
我們現在先建立一個普通的 B.master,
然後刪除其中除了 Page directive 的其他部分。
把 Page Directive 修改為如下,並加入自己要定義的 PlaceHolder:
<%@ Master MasterPageFile="~/A.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="server">
Hello!
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" Runat="server">
</asp:ContentPlaceHolder>
</asp:Content>
用嵌套的模板產生的子頁面將不能採用 VS2005 的 design 模式。
10. 容器特定的 Master Pages
為了能相容不同的瀏覽器,asp.net 2.0 支援多個 Master Page. 在運行時將自動載入合適的 Master Page.
文法如下:
<%@ Page Language="VB" MasterPageFile="~/Abc.master"
Mozilla:MasterPageFile="~/AbcMozilla.master"
Opera:MasterPageFile="~/AbcMozilla.master" %>
11. 頁面請求的次序
當使用者請求一個用 Master Page 構建的頁面時,各種事件發生的次序如下:
Master Page 子控制項初始化;
內容頁面子控制項初始化;
Master Page 初始化;
內容頁面初始化;
內容頁面 Page_Load;
Master Page 的 Page_Load;
Master Page 子控制項載入;
內容頁面子控制項載入;
注意點:
因為內容頁面的 Page_Load 先於 Master Page 的 Page_Load,所以,如果要訪問 Master Page 裡的伺服器控制項,則必須在內容頁面的 Page_LoadComplete 方法裡書寫代碼。
12. 使用緩衝
只有在內容頁面才可以使用如下的 directive 指定緩衝:
<%@ OutputCache Duration="10" Varybyparam="None" %>
(這個指令讓伺服器在記憶體裡緩衝該頁面 10 秒鐘)
如果對 Master Page 指定該指令,本身並不會引發錯誤。但是當他的子頁面下一次來擷取其 Master Page 的時候,如果這時 Master Page 已經到期,則會引發一個錯誤。
所以實際上只能對子頁面指定緩衝。