views:

865

answers:

5

Is it possible to save ViewState information, e.g. to session, so that when you navigate away from the page it is persisted somehow? Then when you return to that page you can reload the view state and the choices you've made are preserved.

Background

I have two pages, a report page, where you can pick data, do some filtering and sorting etc. and a chart-page, where the data you picked from the report page can be presented in different ways, with different choices for presentation.

If the user has tested different presentations, simply using the back-button could mean quite a few clicks before the user's back at the report page. I'd like a direct link to the report page.

  • Using QueryString to save control states is not an option.
  • I can't customize the ViewState storage for the whole application.
+3  A: 

Yes, it's possible to store the Viewstate in something like a database. You just need to implement one of the viewstate providers. See here for an example using the SqlViewStateProvider.

Edit: Just re-read your post, and saw that you said you couldn't customize how the viewstate is stored for the whole application. If that's the case, you might want to look into storing it in a session. Scott Hanselman discusses that here.

Alex Fort
I've looked at this alternative, but as I've understood, this is an application wide implementation? I've stated that changing the entire application is not possible.
chriscena
Yeah, I just saw that. You CAN store the viewstate in a session, but it's not the prettiest thing in the world. See my edited answer.
Alex Fort
+1  A: 

Your link could automatically navigate back the required number of pages using JavaScript. Look at window.history, if you can count the number of pages forward you can navigate back that many.

The ViewState is already designed to persist the state of the user controls. If your user has made a selection and that selection is processed server side with a full page postback the new state of the controls will be saved in the ViewState (hidden input __VIEWSTATE).

If your report is using AJAX and partial page postbacks then you won't get the ViewState on the page anyway.

Dave Anderson
Javascript is probably what we will go for, quick and dirty, but as with saving viewstate in sessions, it does have some obvious limitations.What you mention about viewstate is already known, but the problem here is keeping viewstate while moving between pages.
chriscena
A: 

This is a bad idea, just use querystrings. I would be interested to know why they are not an option.

Shawn Simon
According to the person who wrote the reports page, there are a lot dynamic controls and dependencies (and a lot of smelly code), which would make it time consuming and difficult to add and maintain querystring processing when adding new features.
chriscena
A: 

For all the questions like this (and my own sanity), I wrote a small utility class which will allow you to persist viewstate not only on postbacks but to other pages as well. It also works perfectly in a web farm situation where any server might serve your page.

It is very easy to integrate to your code.. two lines ... and allows you to use property syntax to save and retrieve variables .. myvar.MemberID = "1234" etc.

It includes, DES encryption (turn on/off per page as needed), and a property to let you know if someone tampered with the URL.

Clean and easy ... enjoy

The blog entry has a link to the full sample code. http://designlunacy.blogspot.com/2009/02/easy-web-state-management.html#links

Karl Kemerait - Underhill, VT

Karl
A: 

Just to clarify, the SQLViewstateProvider is NOT an application wide implementation. You have to create a class which inherits from the System.Web.UI.Page object and overrides the Save And Load viewstate methods of the Parent Page class. For each page that you want the viewstate to be saved on server side you then have to inherit from your newly created Page Template (Which in turn inherits and overrides the System.WEb.UI.Page class).

So it is applied on a per-page basis, not on an application-wide basis.

HEADS UP: Some controls might contain some client-side javascript code which may reference the viewstate on client-side (duh). If the viewstate is now stored on server-side you will get a null-reference exception (for instance, clicking a commandfield in a gridview). I'm working on a workaround for this problem, but unfortunately I do not have any concrete solution as of yet.