I have .aspx page that page inserts the data to the database on a button click. But when i press the button it is going right. i m getting the Successfully message as ” successfully inserted data”. In this situation if i press “F5” or Refresh the page it is firing the button click event. Why it should be ? How to avoid this condition ?
Share
When the user clicks F5 (or uses a toolbar button to refresh the page) it will cause a new request, identical to the previous one, to be sent to the server. The
Button.Clickevent will be raised again, but you have a few ways to protect yourself against inserting the data twice.The best way, IMHO, is to use the Post/Redirect/Get pattern. In your code, right after the point where the data is saved, do a 302 redirect to a confirmation page:
When using the pattern, the
POSTto the original page will not end up in the browser history, and refreshing the result page will cause the finalGETto be repeated, which should be safe.