.net – ASP.NET ViewState postback with page refresh and bookmarks

asp.netnetpostpostbackviewstate

The ASP.NET ViewState feature can sometimes be a double edged sword. I rely on it on most of my projects and it speeds up development considerably.

My problem is that sometimes users will try to refresh a page which will cause the viewstate to be lost, other times the user might want to bookmark a page but when the get back the viewstate will be lost.

Modern browsers will display a silly dialog when a user tries to refresh a page that is the result of a POST operation (e.g. asp postback) which is not desirable at all.

I wonder is their a way to continue using ViewState and the postback model but without the drawback of the refresh dialog box. (and if possible bookmark the page.)

An example of what i might want to do is having a page with records and checkboxes next to them, the user has the option to check all the records they want to delete and then click on a delete button. After the user clicks delete the records are analyzed on the server and the new page lists all the records that were selected with a confirm delete button. Now if the user clicks refresh they get this silly box to confirm if they want to post or not.

I understant that ViewState is the result of using the Post Back model which means that most asp.net pages are the result of a POST operation, but i wonder if there are any ways around it.

Workarounds that i thought might work:
In the Page_Unload event save the viewstate in the session with a unique id and redirect the user to the same page with the unique id as a query string parameter, after the page loads with a unique id in the url the viewstate is loaded from the session and injected into the current page. Such a method will allow to user to refresh the page and always get back the same results.

P.S.
I understand that i can use Response.Redirect() and/or query strings but i want to use the simplicity of ViewState

Best Answer

My 2 cents: Instead of using the simplicity of ViewState sandbag use the simplicity of Session - which really is the same simplicity and you won't have to bother worrying about page refresh and stuff. Moreover session is way more flexible since you can use it to store more than just built-in types without affecting performance (you can do it with ViewState as well but you will affect performance).