ViewState存储到服务端
需要把ViewState保存到服务器端的页面继承此类。由于ViewState的数据量太大而产生Client与Server进行交互时的网络传输数据量过大从而导致效率低下的现象。为了解决这个问题可以采用服务器端ViewState的方法,将不影响页面显示的对象保存到服务器端的硬盘上。
using System;
using System.IO;
using System.Threading;
using System.Web.UI;
public class VPage : Page { static private DirectoryInfo Dir = new DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"App_Data\ViewState\"));
static VPage()
{
if (!Dir.Exists)
Dir.Create();
}
protected override object LoadPageStateFromPersistenceMedium()
{
string viewStateID = (string)((Pair)base.LoadPageStateFromPersistenceMedium()).Second;
string stateStr = (string)Cache[viewStateID];
if (stateStr == null)
{
string fn = Path.Combine(Dir.FullName, viewStateID);
stateStr = File.ReadAllText(fn);
}
else
Cache.Remove(viewStateID);
return new ObjectStateFormatter().Deserialize(stateStr);
}
protected override void SavePageStateToPersistenceMedium(object state)
{
string value = new ObjectStateFormatter().Serialize(state);
string viewStateID = (DateTime.Now.Ticks + (long)this.GetHashCode()).ToString(); //产生离散的id号码
string fn = Path.Combine(Dir.FullName, viewStateID);
ThreadPool.QueueUserWorkItem((obj) => File.WriteAllText(fn, value)); //或者只写 File.WriteAllText(fn, value)
Cache.Insert(viewStateID, value);
base.SavePageStateToPersistenceMedium(viewStateID);
}
static private bool _flag = false;
protected override void _disibledevent="color: #000000">
{
}
}
}