MasterPageFile母版頁
如何使用 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 已經過期,則會引發一個錯誤。 所以實際上只能對子頁面指定緩存。
