<%@ Page Language="C#" MasterPageFile="~/MasterPage/Common.master" AutoEventWireup="true" CodeFile="SceneList.aspx.cs" Inherits="Scene_SceneList" Title="韶關旅遊通---旅遊景區列表" %>
this.Master.Page.Header.Title = "aa";
this.Page.Title = "bb";
內容頁一般不需要cs檔案,單獨成一個檔案,然後使用USER_CONTROL
=============================
一 設定Title
1 如果想所有使用了masterpage的頁面都是用一個title ,可以在masterpage頁中設定title,並將內容頁中的title去掉,否則內容頁中的title會將masterpage中的title覆蓋。
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="MasterpageTest.aspx.cs" Inherits="MasterpageTest" Title="Test"%>
改成
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="MasterpageTest.aspx.cs" Inherits="MasterpageTest" %>
2 如果想每個頁面中使用不同等title就比較簡單,在內容頁中設定就可以,masterpage中的title不用去管 ,應為最終會被內容頁中的覆蓋掉。
二 在內容頁取 masterpage中的屬性和欄位
在內容頁中取masterpage中的屬性或欄位應該是比較常用的。建立一個masterpage頁MasterTest.master 和內容頁Test.aspx,在MasterTest.master的後台代碼中添加一個屬性,如下
private string m_Name;
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
然後在內容頁的後台代碼中你會發現不能訪問masterpage中的屬性,這時切換到內容頁的源裡 在上面添加
<%@ MasterType VirtualPath="~/MasterTest.master" %>
再切換到後台中 就可以訪問masterpage中的屬性了。
三 masterpage頁的作用範圍
1 頁面級
通常情況下我們使用masterpage都是頁面級的,就是在每個內容頁中都會來指定masterpage的名字,通過MastPageFile屬性來設定,如下
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" %>
2 應用程式級
這中就是只需在webconfig檔案中做相應配置,全站所有的內容頁都會引用設定的masterpage,如在webconfig中添加如下代碼
<configuration>
<system.web>
<pages masterPageFile="~/Test.master" />
</system.web>
</configuration>
這樣在內容頁中就不用再去設定MastPageFile屬性了,所有的內容頁都會使用Test.master 。如果有些頁面比較特殊需要用其他的masterpage ,可以這是MastPageFile屬性,將會覆蓋在webconfig中的配置。
用這種方法也可以對某些檔案夾中的所有檔案來進行設定,配置如下
<configuration>
<location path="Admin">
<system.web>
<pages masterPageFile="~/ Test.master " />
</system.web>
</location>
</configuration>
Location的path屬性設定路徑
================================
ASP.NET Master Page改變內容頁title方法
在定義好主版頁面以後,有時我們需要改變網頁的標題但是如果直接在主版頁面中更改title屬性又會導致其他的內容頁出現相同的title情況,VS2008中提供了主版頁面的新功能。
1.通過內容頁中的Page指令中Title屬性改變內容頁title:
<%@ Page Language=”C#” MasterPageFile=”~/MyMaster.master” Title=”My Title” %>
2.通過編程改變:前提是<head>標誌必須是運行在伺服器端,即要給它加上runat="server"屬性
void Page_Load()
{
Page.Header.Title="My Title";
}
3.通過內容頁的head預留位置控制項,在VS2008中添加的主版頁面會在頭部有如下按商品asp:ContentPlaceHolder控制項(把主版頁面的title標籤拖到該控制項內)
<asp:ContentPlaceHolder id="head" runat="server">
<title>無標題頁</title>
</asp:ContentPlaceHolder>
而內容頁往往會添加一個對應的asp:Content控制項,只需要改變其中的title標籤內容即可
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<title>無標題頁</title>
</asp:Content>
本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/cxzhq2002/archive/2008/12/17/3539685.aspx