ASP.NET ViewState postback with page refresh and bookmarks

The ASP.NET ViewState function can sometimes be a double-edged sword. I rely on this on most of my projects and greatly speeds up development.

My problem is that sometimes users will try to refresh the page, which will lead to the loss of the view state, otherwise the user may want to add a bookmark to the page, but when the view state is returned.

In modern browsers, a silly dialog will be displayed when the user tries to refresh the page that is the result of a POST operation (for example, asp postback), which is generally undesirable.

Interestingly, their way to continue using ViewState and the postback model, but without the drawback of the update dialog box. (and, if possible, write down the page.)

An example of what I can do is to have a page with entries and checkboxes next to them, the user has the opportunity to check all the entries that they want to delete, and then click the delete button. After the user clicks on deletion, the analyzes are analyzed on the server, and on the new page all the records that were selected using the confirmation button confirm are displayed. Now, if the user clicks on the update, he receives this stupid box to confirm whether they want to send messages or not.

I understand that ViewState is the result of using the Post Back model, which means that most asp.net pages are the result of the POST operation, but I'm wondering if there are any ways to use it.

Workarounds that I thought might work:
In the Page_Unload event, save the viewstate in a session with a unique identifier and redirect the user to the same page with a unique identifier as a parameter of the query string, after the page loads with a unique identifier in the URL, the viewstate is loaded from the viewport and entered on the current page . This method will allow the user to refresh the page and always get the same results.

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

+4
source share
4 answers

My 2 cents: instead of using the simplicity of ViewState, the sandbox uses the simplicity of Session - it is really the same simplicity, and you do not have to worry about worrying about refreshing the page and so on. Moreover, the session is more flexible, because you can use it to store not only built-in types, but also not affecting performance (you can also do this using ViewState, but this will affect performance).

+2
source

I will give my opinion here one by one.

The fact that dialogue warns you of re-fasting is a good IMO thing. What if it was a check page and resubmission, somehow it ultimately recharged the user's credit card. I agree that the wording may be better in dialogue, but we are where we are.

If you think about the behavior of bookmarks, it means that I want to view this page again and again, but most likely my session will end long before this happens. That way you can save the viewstate to a GUID, but then you have to keep that viewstate in the database potentially forever. What if the page changes? This viewstate will now be invalid.

Of course, in your case, do you need a query string parameter that will load your user data from the database and fill the page during page_load as usual? www.my-site.com/Customer.aspx?Id=90401 or something like that.

My suggestion is not to struggle with tools. Hope this helps.

+1
source

@Asaf - I did not vote for your answer, but here are a few reasons:

1) It is impossible to save complex types in the query string 2) The query string is limited to about 2 Kb in IE, which is not so much 3) The incorrect use of the GET method - GET - literally to "receive" data (such as the google search bar), and POST - to "send" data to the server, to manipulate data on the server (for example, contact forms), 4) Everything that relies on Page.IsPostBack would stop working.

+1
source

This may not be an elegant solution, but I think that if you change the form method to GET, it will solve the problem with your bookmark and update it.

This is the same as using query strings, but with the abstraction, VIEWSTATE remains untouched.

-1
source

All Articles