2012年3月26日月曜日

Retrieve viewstate during a GET request

I have a page with controls that specify a filter on a set ofresults. I have used ajax and the update panel to show updated resultsthrough asynchronous request when the filter gets updated. When theuser selects an item in the set they navigate to a different page...when they then hit the back button they return to the filter page butall the controls are all re-initialized.

What I've been trying isgenerating an id for the page on the initial request (as is done andnormally placed in a hidden field for my custom PageStatePersister thatkeeps the viewstate in a database) but then redirecting the user to aurl with the id appended. So when you later hit the back button youhave the id in the URL.


Next LoadViewState is NOT calledunless the request is the result of a postback. Well I've been tryingto initialize the ViewState in the Page_Load by callingLoadPageStateFromPersistenceMedium() and that does result in callingthrough my custom PageStatePersister... But the ViewState dictionary ofthe page does not get updated... what am I missing?

Or is there another way to accomplish not losing the filter values?

Ok...this is going to be hard for me to explain..but I shall try. And i will suggest two solutions. First of all remember client side styles are not persisted unless asked, and viewstate track changes to state only when tracking is enabled. So all your controls are reinitialised because tracking gets enabled after oninit() and then viewstate just deserializes the stored data.

So unless you set trackviewState(), viewstate is not tracking anything. (Once on, cannot be turned off) Once tracking is on..viewstates items can be marked dirty(IsItemdirty()). You can also set an item to be dirty. Basically tracking allows viewstate to monitor which of its elements have changed since TrackViewState() has been called. Statebag ignores the items that are not marked dirty when it comes to saving view state SaveViewState() and hence items not marked dirty are not serialized.

So one of the solution is to markitems dirty which you want to be persisted before that postback. The other is dont do it by ajax but by actual user controls.

Just a question for you to ponder, if you have two identical pages with a textbox and a button with on onclicking code.

as <as:textbox runat="server" text="abc"><asp:button runat="server" text="submit">

with other pages only having change in its text property as text="the quick brown fox jumps over the little lazy dog" Do the two pages have same viewstate?

Read this article if you want to understand viewstate.

http://msdn2.microsoft.com/en-us/library/ms972976.aspx


I appreciate your feedback but I'm not sure how it applies... I'm not having problems writing viewstate... I'm having problems loading the viewstate during a GET request. I'll try articulating my problem differently:

1) I've persisted the viewstate on the server side.
2) The viewstate is identified by a hidden field and so is in the form data during a postback (this already works)
3) On the first GET of a page I generate the identifier and redirect the request to a page with the viewstate identifier appended to the URL
4) By default Viewstate is NOT loaded during a GET request only POSTBACK
5) On the first GET of the URL+ViewstateID, the viewstate does not exist and the page runs through normal initialization
6) On subsequent requests to URL+ViewstateID does exist and I want to load this Viewstate from storage (see 4)
7) I've tried Page.LoadPageStateFromPersistenceMedium() which does call my pagestatepersister and retrieves the viewstate... now how do I apply this to the page instance...? can I ...?


before you move on to the next page, you can either save your viewstate as I said in my previous post and write it to a session and later on retreive the session, and put it back in viewstate.

Although this can be accomplished by plain sessions.

0 件のコメント:

コメントを投稿