views:

1267

answers:

3

Duplicate: http://stackoverflow.com/questions/305684/how-can-i-prevent-database-being-written-to-again-when-the-browser-does-a-reload


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.

+1  A: 

redirect after post

bpapa
I was looking for something that wouldn't lose me my viewstate. Any way to keep it?
Iain M Norman
If the redirection breaks due to a timeout etc., there's still the danger of the user refreshing the inital URL, causing a double POST.
Ates Goral
That's only applicable in certain circumstances.
Phil
+3  A: 

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

Phil
Wouldn't it be cool if ASP.NET had an option to turn on this unique token automatically?
jm
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
@jm: You would generate that unique identifier per page, and not per user (I think?).
Svish
+1  A: 

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