How do I prevent a form that is posted a second time because of a page reload in a browser from being added to my database again with C# Asp.Net.
Thanks, Steven
How do I prevent a form that is posted a second time because of a page reload in a browser from being added to my database again with C# Asp.Net.
Thanks, Steven
HTTP provides a mechanism for avoiding accidental resubmissions - use the POST method in your form tag, not the GET method.
I think it's fair to say you should use the POST method for a request that updates a resource anyway, because it will prevent other undesireable behaviour too, like bookmarking a page that updates your data for example.
you need some unique value that identifies the form/page - perhaps a generated number in a hidden field - and remember/check that is has been processed
One thing you can do is after your first page is submited, you can do a response.redirect back to the same page (thus killing the SUMBIT if refresh is hit).
EDIT: For Spelling.
I will sometimes add the following line "Response.Redirect("ThisPage.aspx");" to the end of a postback handler for a several reasons. If you are updating data held in an external source and have complex interface (Especially one which uses javascript to alter server controls), this forces all the controls to reset and when the onload event is fired the IsPostBack property is set to false. Another side effect is that hitting F5 doesn't resend the command. This may or may not be the right thing in your situation:
Try to use the Post/Redirect/Get idiom: after the postback is handled (in Page_Load
or a code-behind click handler), redirect the page back to itself,
Response.Redirect(Request.Url.ToString(), true);
I am displaying status about the performed action when the form is submitted. If I redirect with Response.Redirect don't I lose my state about the ID of the entry submitted to the database and things like that?