用HttpModule控制或修改页面基类。
最近无事写了一个HttpModule的抽象基类,从这个类继承的HttpModule就能直接对Page对象进行操作,只需要重写OnPrePageExecute方法,然后向该类提供的Page属性注册事件或者修改属性即可。
使用HttpModule是比页面基类更好的方法,更加灵活方便。
1 /// <summary>
2 /// 控制aspx页面的HttpModule通用基类
3 /// </summary>
4 public abstract class PageModule : IHttpModule
5 {
6
7 private HttpApplication _application;
8
9 /// <summary>
10 /// 销毁PageModule实例。
11 /// </summary>
12 public void Dispose()
13 {
14 }
15
16 /// <summary>
17 /// 实现IHttpModule,初始化PageModule
18 /// </summary>
19 /// <param name="context">HttpApplication实例</param>
20 public void Init( HttpApplication context )
21 {
22 _application = context;
23
24 //_application.PostMapRequestHandler += new EventHandler( OnPostMapRequestHandler );
25 _application.PreRequestHandlerExecute += new EventHandler( OnPreRequestHandlerExecute );
26 _application.PostRequestHandlerExecute += new EventHandler( OnPostRequestHandlerExecute );
27
28 }
29
30
31
32 private void OnPreRequestHandlerExecute( object sender, EventArgs e )
33 {
34 _page = Context.Handler as System.Web.UI.Page;
35
36 if ( _page != null )
37 OnPrePageExecute( sender, e );
38
39 }
40
41 /// <summary>
42 /// 当执行页面处理程序之前发生
43 /// </summary>
44 /// <param name="sender">事件源</param>
45 /// <param name="e">事件参数</param>
46 protected virtual void OnPrePageExecute( object sender, EventArgs e )
47 {
48
49 }
50
51
52 private void OnPostRequestHandlerExecute( object sender, EventArgs e )
53 {
54 if ( _page != null )
55 OnPostPageExecute( sender, e );
56 }
57
58 /// <summary>
59 /// 当执行页面处理程序执行完毕时发生
60 /// </summary>
61 /// <param name="sender">事件源</param>
62 /// <param name="e">事件参数</param>
63 protected virtual void OnPostPageExecute( object sender, EventArgs e )
64 {
65
66 }
67
68
69
70
71 private System.Web.UI.Page _page;
72
73 /// <summary>
74 /// 获取当前请求的页面对象
75 /// </summary>
76 protected System.Web.UI.Page Page
77 {
78 get { return _page; }
79 }
80
81 /// <summary>
82 /// 获取当前的请求的Http上下文信息
83 /// </summary>
84 protected HttpContext Context
85 {
86 get { return _application.Context; }
87 }
88
89 /// <summary>
90 /// 获取模块所在的HttpApplication实例
91 /// </summary>
92 protected HttpApplication ApplicationInstanse
93 {
94 get { return _application; }
95 }
96
97 }
98
2 /// 控制aspx页面的HttpModule通用基类
3 /// </summary>
4 public abstract class PageModule : IHttpModule
5 {
6
7 private HttpApplication _application;
8
9 /// <summary>
10 /// 销毁PageModule实例。
11 /// </summary>
12 public void Dispose()
13 {
14 }
15
16 /// <summary>
17 /// 实现IHttpModule,初始化PageModule
18 /// </summary>
19 /// <param name="context">HttpApplication实例</param>
20 public void Init( HttpApplication context )
21 {
22 _application = context;
23
24 //_application.PostMapRequestHandler += new EventHandler( OnPostMapRequestHandler );
25 _application.PreRequestHandlerExecute += new EventHandler( OnPreRequestHandlerExecute );
26 _application.PostRequestHandlerExecute += new EventHandler( OnPostRequestHandlerExecute );
27
28 }
29
30
31
32 private void OnPreRequestHandlerExecute( object sender, EventArgs e )
33 {
34 _page = Context.Handler as System.Web.UI.Page;
35
36 if ( _page != null )
37 OnPrePageExecute( sender, e );
38
39 }
40
41 /// <summary>
42 /// 当执行页面处理程序之前发生
43 /// </summary>
44 /// <param name="sender">事件源</param>
45 /// <param name="e">事件参数</param>
46 protected virtual void OnPrePageExecute( object sender, EventArgs e )
47 {
48
49 }
50
51
52 private void OnPostRequestHandlerExecute( object sender, EventArgs e )
53 {
54 if ( _page != null )
55 OnPostPageExecute( sender, e );
56 }
57
58 /// <summary>
59 /// 当执行页面处理程序执行完毕时发生
60 /// </summary>
61 /// <param name="sender">事件源</param>
62 /// <param name="e">事件参数</param>
63 protected virtual void OnPostPageExecute( object sender, EventArgs e )
64 {
65
66 }
67
68
69
70
71 private System.Web.UI.Page _page;
72
73 /// <summary>
74 /// 获取当前请求的页面对象
75 /// </summary>
76 protected System.Web.UI.Page Page
77 {
78 get { return _page; }
79 }
80
81 /// <summary>
82 /// 获取当前的请求的Http上下文信息
83 /// </summary>
84 protected HttpContext Context
85 {
86 get { return _application.Context; }
87 }
88
89 /// <summary>
90 /// 获取模块所在的HttpApplication实例
91 /// </summary>
92 protected HttpApplication ApplicationInstanse
93 {
94 get { return _application; }
95 }
96
97 }
98