Preventing webform resubmission on browser refresh without losing viewstate

Duplicate: How can I prevent database being written to again when the browser does a reload/back?


Is there a nice elegant way to stop an .aspx page from resubmitting form data if the user requests a refresh from their browser?

Ideally without losing the viewstate.

EDIT: The most elegant solution for me turned out to be wrapping the relevant controls in an asp.net ajax UpdatePanel. Ta everyone.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Generate and insert a unique identifier into the page that’s also stored on the server. Store that when the form is submitted and don’t let that value get passed in multiple times.

UPDATE: This is the only “proper” way to do this. When I say this, I mean storing something on the server side. Anything based on client behaviour is potentially buggy. Those implementations don’t concern themselves with potential browser bugs, incompatibilities, javascript disabled, connection timeouts etc.

Something on the server side needs to know that this particular action has already been performed and stop it on the server side. I stress this, because often this needs to be done to stop a client refreshing and making multiple orders (and potentially multiple bills). This also allows the client to refresh gracefully if the action hasn’t actually been received and acted upon by the server (e.g. in the case of a timeout).

Method 2

There is no elegant way, but there are options. As stated in one of the other answers you can do a HTTP redirect response. Another solution is submitting through means such as Ajax, or through an iframe.

Method 3

redirect after post


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x