現在許多商業公司設有不同部門,而這些部門在公司網站上都有自己的子網站。一般情況下,每個部門根據自己需求維護各自網站。這雖使公司網站豐富多彩,但對用戶瀏覽帶來不便,因各部門子網站未保持一致性造成用戶瀏覽困難。幸運的是,ASP.NET 2.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>