ASP.NET 2.0 - 母版頁(yè)(Master Pages)
母版頁(yè)(Master Pages)為網(wǎng)站內(nèi)的其他頁(yè)面提供模版。
母版頁(yè)(Master Pages)
Master Page 使您有能力為 web 應(yīng)用程序中的所有頁(yè)面(或頁(yè)面組)創(chuàng)建一致的外觀和行為。
Master Page 為其他頁(yè)面提供了模版,帶有共享的布局和功能。Master Page 為內(nèi)容定義了可被內(nèi)容頁(yè)面覆蓋的占位符。而輸出結(jié)果就是 Master Page 和內(nèi)容頁(yè)面的組合。
內(nèi)容頁(yè)包含您希望顯示的內(nèi)容。
當(dāng)用戶請(qǐng)求內(nèi)容頁(yè)時(shí),ASP.NET 會(huì)對(duì)頁(yè)面進(jìn)行合并以生成輸出,輸出結(jié)果對(duì) Master Page 的布局和內(nèi)容頁(yè)面的內(nèi)容進(jìn)行了合并。
Master Page 實(shí)例:
<%@ Master %>
<html> <body> <h1>Standard Header For All Pages</h1><asp:ContentPlaceHolder id="CPH1" runat="server"> </asp:ContentPlaceHolder>
</body> </html>
Master Page 是一張為其他頁(yè)面設(shè)計(jì)的普通 HTML 模版頁(yè)。
@ Master 指令把它定義為一張 master page。
這個(gè) master page 為單獨(dú)的內(nèi)容包含了一個(gè)占位符標(biāo)簽 <asp:ContentPlaceHolder>。
id="CPH1" 屬性標(biāo)識(shí)該占位符,在相同的 master page 中允許多個(gè)占位符。
該 master page 被保存為 "master1.master"。
注釋:該 master page 也能夠包含代碼,允許動(dòng)態(tài)的內(nèi)容。
內(nèi)容頁(yè)實(shí)例:
<%@ Page MasterPageFile="master1.master" %>
<asp:Content ContentPlaceHolderId="CPH1" runat="server">
<h2>Individual Content</h2> <p>Paragrap 1</p> <p>Paragrap 2</p></asp:Content>
上面的內(nèi)容頁(yè)是獨(dú)立的內(nèi)容頁(yè)面之一。
@ Page 指令把它定義為一張標(biāo)準(zhǔn)的內(nèi)容頁(yè)面。
該內(nèi)容頁(yè)面包含了一個(gè)內(nèi)容標(biāo)簽<asp:Content>,該標(biāo)簽引用了母版頁(yè)(ContentPlaceHolderId="CPH1")。
該內(nèi)容頁(yè)被保存為 "mypage1.aspx"。
當(dāng)用戶請(qǐng)求該頁(yè)面時(shí),ASP.NET 就會(huì)將母版頁(yè)與內(nèi)容頁(yè)進(jìn)行合并。
注釋:內(nèi)容文本必須位于 <asp:Content> 標(biāo)簽內(nèi)。該標(biāo)簽外的文本是不被允許的。
帶有控件的內(nèi)容頁(yè)
<%@ Page MasterPageFile="master1.master" %>
<asp:Content ContentPlaceHolderId="CPH1" runat="server">
<h2>W3School</h2>
<form runat="server">
<asp:TextBox id="textbox1" runat="server" />
<asp:Button id="button1" runat="server" text="Button" />
</form>
</asp:Content>
上面的內(nèi)容頁(yè)演示了如何把 .NET 控件插入內(nèi)容頁(yè),就像插入一個(gè)普通的頁(yè)面中。