4

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.

Community
  • 1
  • 1
Iain M Norman
  • 2,075
  • 15
  • 30

3 Answers3

4

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).

Philip Reynolds
  • 9,364
  • 3
  • 30
  • 37
  • 1
    Wouldn't it be cool if ASP.NET had an option to turn on this unique token automatically? – jm. Apr 30 '09 at 21:51
  • Isn't this also tricky if user has multiple browser windows open at once? You potentially need to keep track of multiple unique tokens for each window, or else the user can't submit on OTHER windows once he submits on one. Is there an elegant workaround? – jm. Apr 30 '09 at 22:01
  • @jm: You would generate that unique identifier per page, and not per user (I think?). – Svish Nov 02 '09 at 10:11
  • @Svish Why not simply generate the token on the request, that way no matter how many tabs you have open, it only keeps track of actual requests made (by that I mean specifically button clicks and the like, not literal HTTP Requests for images and other assets). :) – Chiramisu Apr 11 '13 at 08:11
  • @Chiramisu, Not sure what you're talking about, but I meant that it should be generated once per page, or per form actually. So whenever you have a form, it should have an ` – Svish Apr 11 '13 at 10:57
1

redirect after post

bpapa
  • 21,409
  • 25
  • 99
  • 147
1

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.

Per Stilling
  • 868
  • 2
  • 9
  • 19