Getting asp.net to store viewstate in a session, not to increase html

I am trying to get asp.net to store viewstate in a session, and not to increase html.

Now I read that asp.net comes with a SessionPageStatePersister, which can be used instead of the standard HiddenFieldPageStatePersister for this. I was wondering how can I refuse this?

This is what I have so far: I think I need to create a PageAdapter that returns SessionPageStatePersister from the GetStatePersister method and somehow gets the page to use this pageadapter. But the .PageAdapter page only has a getter, so I'm not sure how you installed it.

See the heading "notes" here: http://msdn.microsoft.com/en-us/library/system.web.ui.hiddenfieldpagestatepersister.aspx

Thanks!

+7
source share
3 answers

To use your own PageAdapter class, you need to register it in a .browser file. You need to add (if you haven't already) the App_Browsers directory. Then add the .browser file with the following XML

<browsers> <browser refID="Default"> <controlAdapters> <adapter controlType="System.Web.UI.Page" adapterType="{Your adapter type}" /> </controlAdapters> </browser> </browsers> 

replace {your adapter type} with your adapter type.

More here

Hope this helps.

+5
source

Are you sure you want to do this? There is a problem

  • How do you keep separate pages apart? Of course, you can prefix the session state name on the page, for example. Session ["/default.aspx-Viewstate"], but what happens when a user opens multiple instances of a page?
  • So, to decide that you put a hidden field with, say, a GUID on each page, which you then use as a key. Thus, your session size is growing. And growing. And growing. How do you know if you can / safely remove things?

If you insist on the title of this read, then all you have to do is get the class out of the page and override LoadPageStateFromPersistenceMedium () and SavePageStateToPersistenceMedium (). But you will hate yourself and ultimately destroy it.

Just make sure that HTTP compression is enabled on your server, and please worry about something else.

+8
source

For what it's worth, here I ended up using the code to solve the big image problem: moving the viewstate from html. Just put this in your mypage.aspx.cs:

 // Inspired by: http://aspalliance.com/72 const string ViewStateFieldName = "__VIEWSTATEKEY"; const string RecentViewStateQueue = "RecentViewStateQueue"; const int RecentViewStateQueueMaxLength = 5; protected override object LoadPageStateFromPersistenceMedium() { // The cache key for this viewstate is stored where the viewstate normally is, so grab it string viewStateKey = Request.Form[ViewStateFieldName] as string; if (viewStateKey == null) return null; // Grab the viewstate data from the cache using the key to look it up string viewStateData = Cache[viewStateKey] as string; if (viewStateData == null) return null; // Deserialise it return new LosFormatter().Deserialize(viewStateData); } protected override void SavePageStateToPersistenceMedium(object viewState) { // Serialise the viewstate information StringBuilder _viewState = new StringBuilder(); StringWriter _writer = new StringWriter(_viewState); new LosFormatter().Serialize(_writer, viewState); // Give this viewstate a random key string viewStateKey = Guid.NewGuid().ToString(); // Store the viewstate in the cache Cache.Add(viewStateKey, _viewState.ToString(), null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(Session.Timeout), CacheItemPriority.Normal, null); // Store the viewstate cache key in the viewstate hidden field, so on postback we can grab it from the cache ClientScript.RegisterHiddenField(ViewStateFieldName, viewStateKey); // Some tidying up: keep track of the X most recent viewstates for this user, and remove old ones var recent = Session[RecentViewStateQueue] as Queue<string>; if (recent == null) Session[RecentViewStateQueue] = recent = new Queue<string>(); recent.Enqueue(viewStateKey); // Add this new one so it'll get removed later while (recent.Count > RecentViewStateQueueMaxLength) // If we've got lots in the queue, remove the old ones Cache.Remove(recent.Dequeue()); } 

And for a super-easy way to use SessionPageStatePersister again put this in your mypage.aspx.cs:

 protected override PageStatePersister PageStatePersister { get { return new SessionPageStatePersister(this); } } 
+7
source

All Articles