日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > asp.net >内容正文

asp.net

ASP.NET 2.0 之 Master Page 学习笔记

發(fā)布時(shí)間:2023/11/30 asp.net 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ASP.NET 2.0 之 Master Page 学习笔记 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

如何使用 MasterPage

1. 創(chuàng)建 MasterPage,后綴名 .master, 如 x.master.
??? 其中用 <asp:ContentPlaceHolder /> 定義空位。如:

??? <asp:ContentPlaceHolder ID="ContentPlaceHolder1" Runat="Server">
??? </asp:ContentPlaceHolder>


2. 創(chuàng)建內(nèi)容頁(yè)面。
??? 在 NewItem 對(duì)話框里選擇 "select master page", 選擇上一步創(chuàng)建的 MasterPage.
??? 產(chǎn)生的代碼里, MasterPageFile 屬性指定了 MasterPage 的位置:

??? <%@ Page Language="VB" MasterPageFile="~/x.master" Title="無(wú)標(biāo)題頁(yè)面" %>

??? 頁(yè)面里用 <asp:Content /> 來(lái)添加內(nèi)容到對(duì)應(yīng)的空位:

??? <asp:Content ID="Content1" ContentPlaceHolderId="ContentPlaceHolder1" Runat="Server">
??????? 內(nèi)容
??? </asp:Content/>

??? 內(nèi)容頁(yè)面沒有 <form id="form1" runat="server">


3. 利用 MasterPage 可以使用多種語(yǔ)言來(lái)編寫一個(gè)頁(yè)面的各個(gè)部分。


4. 除了在 <%@ Page %> 里面指定 MasterPage, 也可以在 web.config 指定:

??? <configuration>
??????? <system.web>
??????????? <pages masterPageFile="~/x.master" />
??????? </system.web>
??? </configuration>

??? 這樣定義后,如果創(chuàng)建 Page 時(shí)選擇了 master page, 則在 <%@ Page %> 里面不需要指定即可使用該 MasterPage.
??? 其他頁(yè)面要使用不同的 MasterPage 的話,只要用第一種方法在 Page directive 里面明確的覆蓋 web.config 里的設(shè)置即可。

??? 可以僅對(duì)一組 pages 指定 MasterPage. 下例利用 web.config 的 location 元素,設(shè)定了 Admin 目錄下的頁(yè)面采用的不同的 MasterPage.

??? <configuration>
??????? <location path="Admin">
??????????? <system.web>
??????????????? <pages masterPageFile="~/y.master" />
??????????? </system.web>
??????? </location>
??? </configuration>


5. 在內(nèi)容頁(yè)面如何設(shè)定 Page 的 Title ?

??? 默認(rèn)情況下,Title 在 MasterPage 中指定后,其他具體頁(yè)面就都使用這個(gè) Title.
??? 在具體頁(yè)面,可以有兩個(gè)辦法修改 Title:
???
??? a. <%@ Page Title="test" %>

??? b. 代碼中:

??????? protected void Page_LoadComplete(object sender, EventArgs e)
??????? {
??????????? Master.Page.Title = "Hello";
??????? }

???
6. 訪問 MasterPage 中的屬性和控件。

??? 用 Master 屬性來(lái)訪問。

??? a. 假設(shè) MasterPage 中有一個(gè) Label1, 那么在內(nèi)容頁(yè)面可以這樣:

??????? protected void Page_LoadComplete(object sender, EventArgs e)
??????? {
??????????? string text = (Master.FindControl("Label1") as Label).Text;
??????? }

??????? 頁(yè)面加載的次序:
???????
??????? 要獲取在 MasterPage 的 Page_Load 里面設(shè)定的值,必須在內(nèi)容頁(yè)面的 Page_LoadComplete 中來(lái)寫。

??????? 前面提到的 FindControl() 方法來(lái)查找 MasterPage 中的控件,是一種后期綁定的做法,一般是不安全的。因?yàn)檫@取決于 MasterPage 中是否存在這個(gè) tag,如果被刪除了,則會(huì)導(dǎo)致錯(cuò)誤。
??????? 比較好的做法是,在 MasterPage 中用屬性封裝對(duì)他的控件的訪問;如果用 FindControl(), 則總是檢查其結(jié)果是否為 null.


7. 指定 MasterPage 中的默認(rèn)內(nèi)容

??? 直接在 <asp:ControlPlaceHolder /> 標(biāo)簽之間指定即可。
??? 如果子頁(yè)面不重新指定,則會(huì)采用該默認(rèn)內(nèi)容。


8. 編程的方式指定 Master Page

??? protected void Page_PreInit(object sender, EventArgs e)
??? {
??????? Page.MasterPageFile = "~/x.master";
??? }


9. 嵌套的 Master Page

??? Master Page 可以繼承自更高層次的 Master Page. 但是在 VS2005 中創(chuàng)建這種子 Master Page 的時(shí)候,不會(huì)有默認(rèn)的支持。
??? 假設(shè)有了一個(gè) A.master,
??? 我們現(xiàn)在先創(chuàng)建一個(gè)普通的 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>

??? 用嵌套的模板產(chǎn)生的子頁(yè)面將不能采用 VS2005 的 design 模式。


10. 容器特定的 Master Pages

??? 為了能兼容不同的瀏覽器,asp.net 2.0 支持多個(gè) Master Page. 在運(yùn)行時(shí)將自動(dòng)加載合適的 Master Page.

??? 語(yǔ)法如下:

??? <%@ Page Language="VB" MasterPageFile="~/Abc.master"
??????? Mozilla:MasterPageFile="~/AbcMozilla.master"
??????? Opera:MasterPageFile="~/AbcMozilla.master" %>


11. 頁(yè)面請(qǐng)求的次序

??? 當(dāng)用戶請(qǐng)求一個(gè)用 Master Page 構(gòu)建的頁(yè)面時(shí),各種事件發(fā)生的次序如下:

??? Master Page 子控件初始化;
??? 內(nèi)容頁(yè)面子控件初始化;
??? Master Page 初始化;
??? 內(nèi)容頁(yè)面初始化;
??? 內(nèi)容頁(yè)面 Page_Load;
??? Master Page 的 Page_Load;
??? Master Page 子控件加載;
??? 內(nèi)容頁(yè)面子控件加載;

???
??? 注意點(diǎn):
???
??? 因?yàn)閮?nèi)容頁(yè)面的 Page_Load 先于 Master Page 的 Page_Load,所以,如果要訪問 Master Page 里的服務(wù)器控件,則必須在內(nèi)容頁(yè)面的 Page_LoadComplete 方法里書寫代碼。


12. 使用緩存

??? 只有在內(nèi)容頁(yè)面才可以使用如下的 directive 指定緩存:

??? <%@ OutputCache Duration="10" Varybyparam="None" %>

??? (這個(gè)指令讓服務(wù)器在內(nèi)存里緩存該頁(yè)面 10 秒鐘)

??? 如果對(duì) Master Page 指定該指令,本身并不會(huì)引發(fā)錯(cuò)誤。但是當(dāng)他的子頁(yè)面下一次來(lái)獲取其 Master Page 的時(shí)候,如果這時(shí) Master Page 已經(jīng)過期,則會(huì)引發(fā)一個(gè)錯(cuò)誤。
??? 所以實(shí)際上只能對(duì)子頁(yè)面指定緩存。

轉(zhuǎn)載于:https://www.cnblogs.com/RChen/archive/2006/03/22/356085.html

總結(jié)

以上是生活随笔為你收集整理的ASP.NET 2.0 之 Master Page 学习笔记的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。