转自:http://blog.csdn.net/codingtaoist/article/details/4002476
有时候我们在页面返回、跳转、多步注册的时候,经常需要保存某个页面的状态,以便需要的时候进行加载。例如下情况:
1、主页面为查询页面,选择完查询条件后显示出结果列表,点击某一条后进行单条处理,处理完毕后返回查询页面,而此页面又需要显示上次的浏览结果。通常的做法是将上一次的查询条件保存下来,在返回此页面的时候重新加载数据,但是这样有些问题:有些复杂查询可能很耗时;有些查询条件很多,保存的时候很繁琐。
2、多步操作后,突然又想返回之前某个结果页面,用js脚本去控制它的histroy的时候可能会很麻烦。
我们是否可以将页面的状态保存到Session(或者序列化到数据库中)里,当需要的时候重新恢复这个页面呢?虽说存储到Session里浪费了服务器资源,但如果存储的较少且要求不很高这却是一个可以大大简化我们应用的办法。经过搜索网上一些相关资料,同时分析asp.net的生命周期,将多人的思想整理在一起,我得出下面一个基类,可简化上述问题处理。这里提供源码以及示例(包括VS2003和VS2005)的下载,具体实现可参考代码以及注释。
状态保存的基类如下,保存状态的页面需要继承此类,如果项目中已经有其他页面基类,可将此类修改,让其先继承于你项目中的基类。
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
*
**********************************************************************************************/