IN asp.net when I submit form and refresh it, the data resubmitted again?
Is there a way in C# to trap the page refresh event on page load??
IN asp.net when I submit form and refresh it, the data resubmitted again? Is
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
ASP.NET doesn’t provide a way to do it directly.
There are a few techniques, on the other hand, to avoid duplicate submission:
Redirect after submission. This is the worst one. Even if it avoids duplicate submission, it is not acceptable in a modern web application from the users point of view.
Track submissions per form, per session. When the user submits a form for the first time, remember this in the session. If another submission happens, try to determine if it must be discarded or not (in some cases, it must not; for example, if I edit my answer on StackOverflow once, I would be able to do it twice if I need to).
Disable submission with JavaScript after the first submission. This avoids in some cases the situation when either the user double-clicks the submission button, or clicks it for the first time, waits and thinks that the form was not submitted, thus clicking for the second time. Of course, don’t rely on this one: JavaScript may be disabled, it will work on double-click but not on F5 refresh, and in all cases the technique is not completely reliable.
As an illustration, let’s try to implement the second one.
Let’s say we have a comment box
this.textBoxCommentwhich let the users add a new comment on a page of a blog. The submission is done like this:If the user clicks twice, the comment will be posted twice.
Now, let’s add some session tracking:
In this case, the user will be able to submit a comment only once. Every other comment will be automatically discarded.
The problem is that the user may want to add comments to several blog posts. We have two possible ways to allow that. The easy one is to reset the session variable on every non-postback request, but this will allow the user to submit a post on one page, load another page, than hit refresh on the first one, thus submitting the comment twice but not being able to add a comment on the second page any longer.
The more advanced one is to track in session the list of pages where the comment was submitted.