Reputation: 3344
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
Upvotes: 2
Views: 1922
Reputation: 8077
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.
Upvotes: 5
Reputation: 43560
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.
Upvotes: -1
Reputation: 75427
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);
Upvotes: 2
Reputation: 6218
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:
Upvotes: 3
Reputation: 61233
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
Upvotes: 3