页面恢复

1 using System;
2  using System.Web;
3 using System.Web.SessionState;
4 using System.Web.UI;
5 namespace GentleSoft.MyOffice
6 {
7 ///<summary>
8 /// 页面状态持久化。可保存和恢复页面的ViewState和表单内容
9 ///</summary>
10 publicclass PersistencePage : GentleUtil.Page.Page
11 {
12
13 ///<summary>
14 /// 是否从Session中恢复状态(此值来控制LoadPageStateFromPersistenceMedium的取值规则以及回发事件RaisePostBackEvent执行与否,防止状态恢复时重复执行控件事件)
15 ///</summary>
16 privatebool taoist_IsLoadPageStateFromToSession =false;
17
18 ///<summary>
19 /// 页面状态保存到的Session
20 ///</summary>
21 privatestring taoist_PageStateSession ="";
22
23 ///<summary>
24 /// 保存页面状态(包括ViewState和表单内容)
25 ///</summary>
26 ///<param name="sessionName">存储页面状态的session</param>
27 protectedvoid SavePageStateToSession(string sessionName)
28 {
29
30 this.taoist_PageStateSession = sessionName;
31 object state =base.LoadPageStateFromPersistenceMedium();
32
33 //保存ViewState与表单内容
34 Pair ps =new Pair();
35 ps.First = state;
36 ps.Second =base.DeterminePostBackMode();
37 Session[sessionName] = ps;
38 }
39
40 ///<summary>
41 /// 重写此方法,如果是从Session中恢复状态则无需触发控件事件
42 ///</summary>
43 ///<param name="sourceControl"></param>
44 ///<param name="eventArgument"></param>
45 protectedoverridevoid RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)
46 {
47 if (!taoist_IsLoadPageStateFromToSession)
48 {
49 base.RaisePostBackEvent(sourceControl, eventArgument);
50 }
51 }
52
53 ///<summary>
54 /// 重写此方法,只有taoist_IsLoadPageStateFromToSession为true时才从Session中还原ViewState。
55 ///</summary>
56 ///<returns></returns>
57 protectedoverrideobject LoadPageStateFromPersistenceMedium()
58 {
59 object state;
60
61 if (taoist_IsLoadPageStateFromToSession)
62 {
63 //还原ViewState
64 Pair ps = (Pair)Session[taoist_PageStateSession];
65 state = ps.First;
66 //清空当前Session,保证只能恢复一次,防止无效恢复
67 Session[taoist_PageStateSession] =null;
68 }
69 else
70 state =base.LoadPageStateFromPersistenceMedium();
71
72 return state;
73 }
74
75 ///<summary>
76 /// 重载此方法,增加表单恢复,并设置是否恢复Session
77 ///</summary>
78 ///<returns></returns>
79 protectedoverride System.Collections.Specialized.NameValueCollection DeterminePostBackMode()
80 {
81
82 System.Collections.Specialized.NameValueCollection formContent =base.DeterminePostBackMode();
83
84 if (formContent ==null)
85 {
86 //判断是否返回
87 if (Request.QueryString["taoist_PageStateSession"] !=null)
88 {
89 taoist_PageStateSession = Convert.ToString(Request.QueryString["taoist_PageStateSession"]);
90 if (Session[taoist_PageStateSession] !=null&&!taoist_PageStateSession.Equals(""))
91 {
92 //设置需要还原ViewState
93 this.taoist_IsLoadPageStateFromToSession =true;
94
95 //先还原表单
96 Pair ps = Session[taoist_PageStateSession] as Pair;
97 if (ps !=null)
98 {
99 formContent = ps.Second as System.Collections.Specialized.NameValueCollection;
100 }
101 }
102
103 }
104 else
105 {
106 Session["mypagestate"] =null;
107 }
108 }
109
110 return formContent;
111 }
112
113 }
114 }

 

posted @ 2010-10-20 14:47  mr.cui  Views(369)  Comments(0Edit  收藏  举报