首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

ASP.NET 2.0 巢狀 MasterPage 設計時可視化編輯解決方案

     編輯:關於ASP.NET

ASP.NET 2.0 支援巢狀 MasterPage,但子版面頁在設計檢視模式下無法正常顯示與拖曳控件。本文提供一個繞過限制的實用技巧。

核心思路:執行期動態指定 MasterPage

  1. 建立基類 BasePage,包含 RuntimeMasterPageFile 屬性。
  2. 覆寫 OnPreInit,在執行期依據屬性值設定 MasterPageFile。

BasePage 代碼

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);
    }
}

版面頁結構

  • MainMaster.master:根版面,含 Header、Footer 與 MainContent ContentPlaceHolder。
  • SubMaster.master:繼承 MainMaster,內含兩欄表格,各放一個 ContentPlaceHolder(LeftColumn、RightColumn)。

內容頁面指令

<%@ Page Language="C#" MasterPageFile="" 
    RuntimeMasterPageFile="SubMaster.master" 
    CodeFileBaseClass="BasePage" ... %>
  • MasterPageFile 留空 → 設計時不套用版面,可正常編輯。
  • RuntimeMasterPageFile 指定執行期套用的版面。
  • CodeFileBaseClass 指向 BasePage 啟用動態切換邏輯。

注意

微軟官方承諾在下一版 Visual Studio 完整支援巢狀 MasterPage 設計時體驗。此法為暫時性繞過方案,實測於 VS2005/IIS6 有效。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved