扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
protected override PageStatePersister PageStatePersister { get { return new SessionPageStatePersister(Page); } } |
protected override object LoadPageStateFromPersistenceMedium() { return Session["ViewState"]; } protected override void SavePageStateToPersistenceMedium(object viewState) { Session["ViewState"] = viewState; RegisterHiddenField("__VIEWSTATE", ""); } |
protected override void SavePageStateToPersistenceMedium(object state) { string viewStateID = "VIEWSTATE#" + Session.SessionID.ToString() + "#" + DateTime.Now.Ticks.ToString(); ClientScript.RegisterHiddenField("__VIEWSTATE_KEY", viewStateID); ClientScript.RegisterHiddenField("__VIEWSTATE","");//请注意 try { if (losFormatter == null) { losFormatter = new LosFormatter(); } StringWriter sw = new StringWriter(); losFormatter.Serialize(sw, state); Common.ViewStateData vsd = new ViewStateData(); vsd.ViewStateID = viewStateID; vsd.ViewState = sw.ToString(); da = new DataAccess(); string error = da.SaveViewState(vsd); Response.Write(error); } catch (Exception ex) { Response.Write(ex.Message); } } protected override object LoadPageStateFromPersistenceMedium() { string viewState = string.Empty; try { if (losFormatter == null) { losFormatter = new LosFormatter(); } string stateID = Page.Request["__VIEWSTATE_KEY"].ToString(); da = new DataAccess(); viewState = da.LoadViewState(stateID); } catch {} return losFormatter.Deserialize(viewState); } |
<input type="hidden" name="__VIEWSTATE" value="" /> |
namespace PageAdapter { using System; using System.IO; using System.Security.Permissions; using System.Web; using System.Web.UI; [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)] public class DatabasePageStatePersister : PageStatePersister { public DatabasePageStatePersister(Page page): base(page) {} // // Load ViewState and ControlState. // public override void Load() { string viewState; IStateFormatter formatter = this.StateFormatter; DataAccess da = new DataAccess(); string stateID = base.Page.Request["__VIEWSTATE_KEY"].ToString(); viewState = da.LoadViewState(stateID); Pair statePair = (Pair)formatter.Deserialize(viewState); ViewState = statePair.First; ControlState = statePair.Second; } // // Persist any ViewState and ControlState. // public override void Save() { if (ViewState != null || ControlState != null) { if (Page.Session != null) { string viewStateID = "VIEWSTATE#" + base.Page.Session.SessionID.ToString() + "#" + DateTime.Now.Ticks.ToString(); base.Page.ClientScript.RegisterHiddenField("__VIEWSTATE_KEY", viewStateID); Pair statePair = new Pair(ViewState, ControlState); IStateFormatter formatter = this.StateFormatter; // Serialize the statePair object to a string. string serializedState = formatter.Serialize(statePair); ViewStateData vsd = new ViewStateData(); vsd.ViewStateID = viewStateID; vsd.ViewState = serializedState; DataAccess da = new DataAccess(); string error = da.SaveViewState(vsd); } else throw new InvalidOperationException("Session needed for StreamPageStatePersister."); } } } } |
protected override PageStatePersister PageStatePersister { get { return new DatabasePageStatePersister(Page); } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者