tags:

views:

693

answers:

7

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

A: 

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.

Brabster
sometimes when you reload in IE it re-posts!
Steven A. Lowe
Really? What version? Never seen it myself. It's not supposed to lol
Brabster
-1, AFAIK when using POST, if the server doesn't redirect you and you reload you will always get the "resubmit" warning.
orip
Don't understand why the downvote - is my answer wrong?
Brabster
i think it's safe to assume that if he's using a form in asp.net, it is already using a POST (just guessing, i didn't downvote your answer)
Steven A. Lowe
+3  A: 

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

Steven A. Lowe
Oh yeah... Good idea. Let me try it. One should not try to program during the same time Christmas concerts are going on taking up all the free time for rehearsals.
Steven Behnke
Thanks. It worked great.
Steven Behnke
+5  A: 

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.

Chu
+3  A: 

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:

Paul
+1  A: 

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);
orip
A: 

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?

Steven Behnke
A: 

Use in end Response.Redirect("Address of Page");