ラベル viewstate の投稿を表示しています。 すべての投稿を表示
ラベル viewstate の投稿を表示しています。 すべての投稿を表示

2012年3月26日月曜日

Retaining ViewState in Atlas

On my real estate website I have a Search page and a Results page. The Search page contains three linked Atlas cascading dropdowns (CDDs). The user makes some selections on these and then clicks a <search> button to see the results. This opens the Results page, which displays a list of properties meeting the search criteria. I have this working fine, apart from one thing: when the user goes back to the Search page, the CDDs have been reset to their original values. I was wondering if there was an easy way for the Search page to retain its settings. I have a partial solution, but it's not very good. If I set AutoPostBack to "True" for each CDD, they retain their settings when I click the <back> button on the browser. This is pretty horrible though, as it means the whole page gets posted back - undermining the whole purpose of using Atlas in the first place! I have tried enclosing the CDDs in an UpdatePanel (so that only they get posted back and not the whole page), but they just end up flickering all the time as (I guess) they get stuck in a code loop.

Also, setting AutoPostBack to "True" is only effective when the user clicks the browser's <back> button. I want to offer my own button or link in case they arrived at the Results page from another route. I have tried creating a hyperlink to the Search page, adding a button with a PostBackUrl of "Search.aspx" and adding a Response.Redirect command to a button's on_click event. Even with AutoPostBack set to "True" (which I want to avoid), none of these methods works. The CDDs just get reset every time.

Also, the other server controls I have on the Search page (check boxes, conventional drop-down list boxes, etc.) get reset every time too. I've tried looking into ViewState, but I can't get it to work. It looks like I need a way to save the viewstate on exit and somehow retrieve it when I load the page again. I guess I'm missing something fundamental. Any suggestions? Am I barking up the wrong tree? I imagine this kind of thing must have been done many times before and would appreciate a few hints.Any ideas on this? I'd really appreciate some help on something that must be a fairly common problem - or will be as Atlas takes off. Thanks for your interest.

You could try saving the viewstate to sessionstate or to disk

An example of saving viewstate to disk can be found here :
http://aspalliance.com/911


That seems to be on the right track. I'm now saving the values of my page controls to Session State when I exit the page, and retrieving them when I load it. This works fine for all the controls except the CDDs. Since these are populated by WebMethods, I don't seem to have any opportunity to set their SelectedValue properties. To focus on the problem area, I have tried adding a button to my form with a single line of code reading:ddlRegion.SelectedValue = RegionIDWhere RegionID is a value retrieved from the session.This causes an argument out of range exception as follows:[ArgumentOutOfRangeException: 'ddlRegion' has a SelectedValue which is invalid because it does not exist in the list of items.Parameter name: value]I get the same error if I change the variable RegionID to the literal "1" (which is definitely in the list).What it boils down to is this:How can I programatically set the SelectedValue of an Atlas Cascading Dropdown?I know how to save the value to, and retrieve it from, session state, but I don't know how to assign it to an Atlas cascading dropdown list. Any ideas?

1. Perest the CCD by setting the selected value of the cascading extender control ( and not the drop-down) , as explained in the article ( link )

2. and you can retrieve the viewsate ... as explained on this link.http://gandhirohan.blogspot.com/2007/09/cascadingdropdown-control.html

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.