重写Page类中的LoadPageStateFromPersistenceMedium()和SavePageStateToPersistenceMedium(object state)方法。
Demo:将ViewState存放到Session中:
protected LosFormatter losFormatter;
protected override object LoadPageStateFromPersistenceMedium()
{
string key = Request.RawUrl + "__viewstate";
if (Session[key] != null)
{
MemoryStream stream = (MemoryStream)Session[key];
stream.Seek(0, SeekOrigin.Begin);
return losFormatter.Deserialize(stream);
}
return null;
}
protected override void SavePageStateToPersistenceMedium(object state)
{
string key = Request.RawUrl + "__viewstate";
MemoryStream stream = new MemoryStream();
losFormatter.Serialize(stream, state);
stream.Flush();
Session[key] = stream;
}