嵌套MasterPage 可视化编辑

知识了解:
Page.OnPreInit的方法 :在页初始化开始时引发 PreInit 事件。
说明:在调用 OnPreInit 方法后,将加载个性化信息并初始化页主题(如果有)。这也是为页动态定义 PageTheme 或MasterPage 的首选阶段

我这里的 加载 嵌套MasterPage 就是在这个事件里面处理。原理是 在页面编辑过程中看到的MasterPage是假的,真正的路径是通过此事件加载来的。

介绍<@ Page 中的一个属性CodeFileBaseClass。
CodeFileBaseClass :指定页的基类及其关联的代码隐藏类的路径。页面的分部类(其名称分配给 Inherits 属性,并且其源文件由 CodeFile
属性引用)是从该基类继承的,则该基类中的字段在代码生成之后将能够引用页上的控件。
使用此属性,就可以自己定义 属性 并在<@ Page 中加入。
操作如下:
1。那么 我们来看代码CodeFileBaseClass 的类,我们这里定义为BasePage

 

public class BasePage:System.Web.UI.Page
{
    
private string runtimeMasterPageFile;

    
public string RuntimeMasterPageFile       //此属性 就可以在〈@Page 里面加入,并且就是想要真正加载的嵌套MasterPage路径     
    {
        
get { return runtimeMasterPageFile;     }
        
set { runtimeMasterPageFile = value;    }
    }
    
protected override void OnPreInit(EventArgs e)  //重新加载模版路径
    { 
        
if (runtimeMasterPageFile != null
        { 
            
this.MasterPageFile = runtimeMasterPageFile; 
        } 
        
base.OnPreInit(e); 
    } 
}

2。在页面的cs文件中,修改继承关系:public partial class 页的类名 : BasePage
3。添加嵌套MasterPage, 和 假的MasterPage(但是必须引用嵌套MasterPage的contentplaceholder.Id)
则 页面的 头信息里是 :<%@ Page Language="C#" MasterPageFile="假的MasterPage的路径" RuntimeMasterPageFile="嵌套MasterPage的路
径"  CodeFileBaseClass="BasePage"   AutoEventWireup="true" CodeFile="文件的cs" Inherits="cs类名"%>
  这样此页面就可以切换到设计模式进行编辑。

posted on 2007-10-09 10:23  张敏  阅读(311)  评论(0编辑  收藏  举报