页面状态保存

有时候我们在页面返回、跳转、多步注册的时候,经常需要保存某个页面的状态,以便需要的时候进行加载。例如下情况:

1、主页面为查询页面,选择完查询条件后显示出结果列表,点击某一条后进行单条处理,处理完毕后返回查询页面,而此页面又需要显示上次的浏览结果。通常的做法是将上一次的查询条件保存下来,在返回此页面的时候重新加载数据,但是这样有些问题:有些复杂查询可能很耗时;有些查询条件很多,保存的时候很繁琐。

2、多步操作后,突然又想返回之前某个结果页面,用js脚本去控制它的histroy的时候可能会很麻烦。

       我们是否可以将页面的状态保存到Session(或者序列化到数据库中)里,当需要的时候重新恢复这个页面呢?虽说存储到Session里浪费了服务器资源,但如果存储的较少且要求不很高这却是一个可以大大简化我们应用的办法。经过搜索网上一些相关资料,同时分析asp.net的生命周期,将多人的思想整理在一起,我得出下面一个基类,可简化上述问题处理。这里提供源码以及示例(包括VS2003和VS2005)的下载,具体实现可参考代码以及注释。

       状态保存的基类如下,保存状态的页面需要继承此类,如果项目中已经有其他页面基类,可将此类修改,让其先继承于你项目中的基类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;  
using System.Web;  
using System.Web.SessionState;  
using System.Web.UI;  
/// <summary>  
/// 页面状态持久化。可保存和恢复页面的ViewState和表单内容  
/// </summary>  
public class PersistencePage : System.Web.UI.Page  
{  
   
    /// <summary>  
    /// 是否从Session中恢复状态(此值来控制LoadPageStateFromPersistenceMedium的取值规则以及回发事件RaisePostBackEvent执行与否,防止状态恢复时重复执行控件事件)  
    /// </summary>  
    private bool taoist_IsLoadPageStateFromToSession = false;  
   
    /// <summary>  
    /// 页面状态保存到的Session  
    /// </summary>  
    private string taoist_PageStateSession = "";  
   
    /// <summary>  
    /// 保存页面状态(包括ViewState和表单内容)  
    /// </summary>  
    /// <param name="sessionName">存储页面状态的session</param>  
    protected void SavePageStateToSession(string sessionName)  
    {  
   
        this.taoist_PageStateSession = sessionName;  
        object state = base.LoadPageStateFromPersistenceMedium();  
   
        //保存ViewState与表单内容  
        Pair ps = new Pair();  
        ps.First = state;  
        ps.Second = base.DeterminePostBackMode();  
        Session[sessionName] = ps;  
    }  
   
    /// <summary>  
    /// 重写此方法,如果是从Session中恢复状态则无需触发控件事件  
    /// </summary>  
    /// <param name="sourceControl"></param>  
    /// <param name="eventArgument"></param>  
    protected override void RaisePostBackEvent(IPostBackEventHandler sourceControl, string eventArgument)  
    {  
        if (!taoist_IsLoadPageStateFromToSession)  
        {  
            base.RaisePostBackEvent(sourceControl, eventArgument);  
        }  
    }  
   
    /// <summary>   
    /// 重写此方法,只有taoist_IsLoadPageStateFromToSession为true时才从Session中还原ViewState。   
    /// </summary>   
    /// <returns></returns>   
    protected override object LoadPageStateFromPersistenceMedium()  
    {  
        object state;  
   
        if (taoist_IsLoadPageStateFromToSession)  
        {  
            //还原ViewState  
            Pair ps = (Pair)Session[taoist_PageStateSession];  
            state = ps.First;  
            //清空当前Session,保证只能恢复一次,防止无效恢复  
            Session[taoist_PageStateSession] = null;  
        }  
        else 
            state = base.LoadPageStateFromPersistenceMedium();  
   
        return state;  
    }  
   
    /// <summary>   
    /// 重载此方法,增加表单恢复,并设置是否恢复Session  
    /// </summary>   
    /// <returns></returns>   
    protected override System.Collections.Specialized.NameValueCollection DeterminePostBackMode()  
    {  
   
        System.Collections.Specialized.NameValueCollection formContent = base.DeterminePostBackMode();  
   
        if (formContent == null)  
        {  
            //判断是否返回  
            if (Request.QueryString["taoist_PageStateSession"] != null)  
            {  
                taoist_PageStateSession = Convert.ToString(Request.QueryString["taoist_PageStateSession"]);  
                if (Session[taoist_PageStateSession] != null && !taoist_PageStateSession.Equals(""))  
                {  
                    //设置需要还原ViewState  
                    this.taoist_IsLoadPageStateFromToSession = true;  
   
                    //先还原表单  
                    Pair ps = Session[taoist_PageStateSession] as Pair;  
                    if (ps != null)  
                    {  
                        formContent = ps.Second as System.Collections.Specialized.NameValueCollection;  
                    }  
                }  
   
            }  
        }  
   
        return formContent;  
    }  
   
}  
/******************************************************************************************************************** 
    * 说    明:需要保存状态的页面需要继承此类,使用方法如下: 
    *  
                1、保存状态时使用SavePageStateToSession方法,例子: 
                private void Button2_Click(object sender, System.EventArgs e) 
                {    
                        this.SavePageStateToSession("mypagestate"); 
                        Response.Redirect("WebForm2.aspx");  
                
                mypagestate 为要将页面状态保存到的Session的名称 
                   
                2、恢复状态需向页面传递taoist_PageStateSession参数,例子 
                private void Button1_Click(object sender, System.EventArgs e) 
                
                    Response.Redirect("WebForm1.aspx?taoist_PageStateSession=mypagestate"); 
                
                参数名为taoist_PageStateSession,其值为要恢复WebForm1.aspx页面所使用的Session 
    *  
*************************************************************************************************************************/ 
<br>本文来自CSDN博客:<a href="http://blog.csdn.net/codingtaoist/archive/2009/03/18/4002476.aspx" rel="noopener nofollow">http://blog.csdn.net/codingtaoist/archive/2009/03/18/4002476.aspx</a>
posted @   ChineseCheng  阅读(1285)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示