当前位置:编程学习 > asp >>

重写page 基类的几个方法

1、继承Page,重写OnLoad方法(微软推荐,但不推荐页面基类)

protected   override   void   OnLoad(EventArgs   e)
{
      ……
}
2、继承Page,在构造函数中注册事件:
    this.Load   +=   new   EventHandler(   Page_Load   );
    然后写Page_Load方法。
 
3、推荐解决方案:利用HttpModule把代码挂到Page的Load事件上。 
//// <summary>
/// 控制aspx页面的HttpModule通用基类
/// </summary>
public abstract class PageModule : IHttpModule
{

    private HttpApplication _application;

    /**//// <summary>
    /// 销毁PageModule实例。
    /// </summary>
    public void Dispose()
    {
    }

    /**//// <summary>
    /// 实现IHttpModule,初始化PageModule
    /// </summary>
    /// <param name=”context”>HttpApplication实例</param>
    public void Init( HttpApplication context )
    {
      _application = context;

      //_application.PostMapRequestHandler += new EventHandler( OnPostMapRequestHandler );
      _application.PreRequestHandlerExecute += new EventHandler( OnPreRequestHandlerExecute );
      _application.PostRequestHandlerExecute += new EventHandler( OnPostRequestHandlerExecute );

    }

  
    private void OnPreRequestHandlerExecute( object sender, EventArgs e )
    {
      _page = Context.Handler as System.Web.UI.Page;

      if ( _page != null )
        OnPrePageExecute( sender, e );

    }

    /**//// <summary>
    /// 当执行页面处理程序之前发生
    /// </summary>
    /// <param name=”sender”>事件源</param>
    /// <param name=”e”>事件参数</param>
    protected virtual void OnPrePageExecute( object sender, EventArgs e )
    {

    }

  
    private void OnPostRequestHandlerExecute( object sender, EventArgs e )
    {
      if ( _page != null )
        OnPostPageExecute( sender, e );
    }

    /**//// <summary>
    /// 当执行页面处理程序执行完毕时发生
    /// </summary>
    /// <param name=”sender”>事件源</param>
    /// <param name=”e”>事件参数</param>
    protected virtual void OnPostPageExecute( object sender, EventArgs e )
    {

    }

    private System.Web.UI.Page _page;

    /**//// <summary>
    /// 获取当前请求的页面对象
    /// </summary>
    protected System.Web.UI.Page Page
    {
      get { return _page; }
    }

    /**//// <summary>
    /// 获取当前的请求的Http上下文信息
    /// </summary>
    protected HttpContext Context
    {
      get { return _application.Context; }
    }

    /**//// <summary>
    /// 获取模块所在的HttpApplication实例
    /// </summary>
    protected HttpApplication ApplicationInstanse
    {
      get { return _application; }
    }

}

补充:Web开发 , ASP.Net ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,