現在的很多商業公司都設有不同的部門,而這些部門在公司的網站上都有自己的子網站。一般情況下,每一個部門都會根據自己的需要來維護各自的網站。這樣做雖然會使公司的網站顯得豐富多彩,但這卻會對使用者的訪問帶來不便,也就是說,由於各個部門的子網站沒有保持一致性而使使用者在瀏覽網站時造成了困難。幸運的是,ASP.NET2.0為我們提供了一種解決方案,這就是首頁嵌套。
建立嵌套首頁
首先需要建立一個標準的首頁,在這個首頁上需要加上一些共用的東西,如公司的Logo、公司名稱、頁尾以及菜單等,而每個部門的子網站必須使用這個標準的首頁。每一個部門可以根據自己的業務需要建立各自的首頁,然後將這些部門的首頁嵌入剛才建立的標準中。這樣做無論對最終的使用者還是對每個部門都是有好處的,對於終端使用者,無論他訪問哪個部門的網站,都會看到同樣的Logo、菜單以及頁尾。而對於部門來說,他們可以自己建立一個內嵌的首頁,這個首頁的內容可以根據自己部門的業務需要而定。這就象是一個OCX控制嵌在網頁上一樣。
為了理解如何建立嵌套首頁,下面讓我們看一個例子。首先給出一個標準首頁的例子。
以下是引用片段: <%@ Master Language="C#" AutoEventWireup="true" CodeFile="WebsiteMasterPage.master.cs" Inherits="WebsiteMasterPage" %> <html> <head runat="server" id="head"> <title>標準首頁例子</title> </head> <body> <form id="form1" runat="server"> <table width="100%"> <tr> <td bgcolor="#006633" colspan="2"> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td align="left"> <a href="/MasterPage/Default.aspx"> <img alt="Home Page" border="0" src="/MasterPage/images/logo.gif" /> </a> </td> <td align="right"> <img src="/MasterPage/images/header_image.gif"/> </td> </tr> </table> </td> </tr> <tr> <td width="25%"> <font color="#3300FF">部門1 <br /> 部門2 <br /></font> </td> <td width="75%"> <asp:ContentPlaceHolder ID="Main" runat="server"> </asp:ContentPlaceHolder> </td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td bgcolor="#0000FF" colspan="2"> <font color="#FFFF00">註腳</font> </td> </tr> </table> </form> </body> </html> |
上面的標準首頁定義了公司的Logo、註腳和菜單的位置。還定義了部門的首頁要嵌入的位置(這個要使用ContentPlaceHolder控制項)。部門首頁的代碼中和上面的代碼有些不同,在部門首頁的代碼中需要引用上述的標準首頁。這個可以通過在部門首頁代碼中加入MasterPageFile屬性實現。下面是一個部門的首頁代碼:
以下是引用片段: <%@ Master MasterPageFile="~/Templates/WebsiteMasterPage.master" Language="C#" AutoEventWireup="true" CodeFile="NestedMasterPage.master.cs" Inherits="NestedMasterPage" %> <asp:Content ID="Content1" ContentPlaceHolderID="Main" runat="server"> <table width="100%"> <tr> <td style="background-color:BLUE; font-weight: bold; coloar: white"> <font color="#FFFF00" >部門首頁 </font> </td> </tr> <tr> <td> <asp:ContentPlaceHolder ID="NestedMain" runat="server" /> </td> </tr> </table> </asp:Content> 從上面的代碼可以看出其中引用了標準首頁WebsiteMasterPage.master。而且還定義了一個服務端控制項來引用在標準首頁中定義的ContentPlaceHolder的ID(ID為Main)。由於部門首頁被嵌套在標準首頁中,因此,必須使用Content服務端控制項。還有就是必須加入ContentPlaceHolder控制項,這個控制項指示了部門首頁顯示的位置。 現在這個部門首頁已經被嵌入到標準首頁中了。部門的首頁可以自動繼承標準首頁的Logo、註腳和菜單。如果要更換這些公用的元素,只需更新這個標準首頁即可。而且各部門也可根據自己的需要來更新內嵌在標準首頁的部門首頁。程式運行介面1所示。 圖1 在Visual Studio2005中使用嵌套首頁 我們從上面的部門首頁代碼中可以看到,MasterPageFile屬性引用了標準首頁。但這個屬性在Visual Studio2005中並不支援可視化編輯。因此,要想在VS2005的設計檢視中編輯首頁,必須將MasterPageFile設為空白串。如下面的代碼如示:
以下是引用片段: <%@ Page Language="C#" MasterPageFile="" Title="部門首頁" %> |
當我們將MasterPageFile設為空白串後,在每次更新標準首頁後在發布時都得手工來修改這個屬性。如果不想這麼麻煩的話,可以通過一些手段來欺騙一個Visual Studio .NET的設計檢視。首先建立一個從System.Web.UI.Page繼承的類(將這個類命名為BasePage)。在這個類中定義一個RuntimeMasterPageFile屬性(這個屬性可以使用任何名子)。實現代碼如下:
以下是引用片段: public class BasePage : System.Web.UI.Page { private string _RuntimeMasterPageFile; public string RuntimeMasterPageFile { get { return _RuntimeMasterPageFile; } set { _RuntimeMasterPageFile = value; } } protected override void OnPreInit(EventArgs e) { if (_RuntimeMasterPageFile != null) { this.MasterPageFile = _RuntimeMasterPageFile; } base.OnPreInit(e); } } |
BasePage還重載了OnPreInit方法,以便在Aspx頁裝載時可以動態地設定MasterPageFile屬性。實現了BasePage類後,以後需要內嵌的aspx中的類就可以直接從BasePage繼承了。
以下是引用片段: public partial class MyNestedMaster : BasePage { // 具體的實現代碼 } |
下面我們來修改.aspx檔案。首先將MasterPageFile屬性設為空白串,並且將RuntimeMasterPageFile屬性直接加到aspx檔案中,並將它的值設為內嵌首頁的路徑。然後設定CodeFileBaseClass屬性為"BasePage",實現代碼如下:
以下是引用片段: <%@ Page Language="C#" MasterPageFile="" RuntimeMasterPageFile="~/Templates/NestedMasterPage.master" CodeFileBaseClass="BasePage" Inherits="MyNestedMasterAndBasePage" AutoEventWireup="true" CodeFile="MyNestedMasterAndBasePage.aspx.cs Title="Page1" %> <asp:Content ID="ContentNested" runat="server" ContentPlaceHolderID="NestedMain"> <p> </p> <p> </p> 財務部首頁 <p> </p> <p> </p> </asp:Content> |
在運行時,BasePage類將被執行個體化,而MasterPageFile屬性將被動態地設定為RuntimeMasterPageFile屬性的值。 |