I have command button added in my asp.net grids. After performing an action using that button, we refresh the grid to reflect the new data. (basically this action duplicates the grid row).
Now when user refresh the page using F5, an alert message is displayed (to resend the information to server) if we select "retry", the action is repeated automatically.
I know this is a common problem in asp.net, how can we best handle this?
A refresh mean a complete reload of the page, without any form data. This is essentially an HTTP GET . A post back is when the page is posted to itself (through the form action="" ). This is essentially an HTTP POST .
After a Form submission, when User refreshes the Page or presses the F5 key, the Browser tries to re-post the information again to the Browser and if the User responds as Retry then it could lead to duplicate entries in the Database.
Response. Write("<script type='text/javascript'> setTimeout('location. reload(true); ', timeout);</script>"); Put the above code in button click event or anywhere you want to force page refresh.
One way to prevent this from happening is to use Response. Redirect("same_page") to the same page after the event logic. This will force the page to reload and thereafter doing any further page refreshes would not call the button click event.
Search for GET after POST - http://en.wikipedia.org/wiki/Post/Redirect/Get - basically, redirect to the current page after you're finished processing your event.
Something like:
Response.Redirect(Request.RawUrl)
If you think you don't need postback paradigm, you might want to look at ASP.NET MVC.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With