扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:子非鱼 来源:博客园 2007年11月6日
关键字: Windows
namespace StreamPageAdapter { using System; using System.IO; using System.Security.Permissions; using System.Web; using System.Web.UI; // // The StreamPageStatePersister is an example view state // persistence mechanism that persists view and control // state on the Web server. // [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)] public class StreamPageStatePersister : PageStatePersister { public StreamPageStatePersister(Page page): base(page) {} // // Load ViewState and ControlState. // public override void Load() { Stream stateStream = GetSecureStream(); // Read the state string, using the StateFormatter. StreamReader reader = new StreamReader(stateStream); IStateFormatter formatter = this.StateFormatter; string fileContents = reader.ReadToEnd(); // Deserilize returns the Pair object that is serialized in // the Save method. Pair statePair = (Pair)formatter.Deserialize(fileContents); ViewState = statePair.First; ControlState = statePair.Second; reader.Close(); stateStream.Close(); } // // Persist any ViewState and ControlState. // public override void Save() { if (ViewState != null || ControlState != null) { if (Page.Session != null) { Stream stateStream = GetSecureStream(); StreamWriter writer = new StreamWriter(stateStream); IStateFormatter formatter = this.StateFormatter; Pair statePair = new Pair(ViewState, ControlState); // Serialize the statePair object to a string. string serializedState = formatter.Serialize(statePair); writer.Write(serializedState); writer.Close(); stateStream.Close(); } else throw new InvalidOperationException("Session needed for StreamPageStatePersister."); } } // Return a secure Stream for your environment. private Stream GetSecureStream() { string path = @"d:\a.txt"; FileStream fs = new FileStream(path, FileMode.Open, FileAccess.ReadWrite); return fs; } } } 再重写PageStatePersister属性就可以了: protected override PageStatePersister PageStatePersister { get { return new StreamPageStatePersister (Page); } |
using System.Security.Permissions; using System.Web; using System.Web.UI; namespace PageAdapter { [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)] public class MyPageAdapter : System.Web.UI.Adapters.PageAdapter { public override PageStatePersister GetStatePersister() { return new PageAdapter.DatabasePageStatePersister(Page); } } } |
<browsers> <browser refID="Default" > <controlAdapters> <adapter controlType="System.Web.UI.Page" adapterType="PageAdapter.MyPageAdapter" /> </controlAdapters> </browser> </browsers> |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者