Not allow resubmit of page

2019-05-28 14:07发布

问题:

I have an ASP.NET 4.0 web form. There is a textbox I want users to submit and check. If it's correct, I blank out the textbox and don't want users to be able to resubmit that data. In IE8, users can hit back (get a resubmit warning) and if they refresh, it will resubmit the data from that text box.

I've tried setting the value to null and disabling viewstate for the textbox.

How do I stop resubmit?

回答1:

You can implement the Post Redirect Get pattern as detailed here on Wikipedia to remove the refresh and double postback.



回答2:

If it's absolutely vital that submissions never get processed twice, combine all of the above.

  1. Put some onclick javascript on the submit button to disable or hide it, as per Andre's comment
  2. Implement Post/Redirect/Get as per Daz Lewis's link to ensure that hitting refresh on the browser won't resubmit the form
  3. Finally, add a hidden field, session variable, etc., as per Matt Greer and Alison's suggestion, so if something does get through - e.g. connection times out in between the post and redirect, and user hits refresh on browser - you don't double-process it at your back end.


回答3:

I don't think that regarding to the Back button you have any choice but to check the value on form submit again, and determine that if it is the resubmitted form or not. You have one other choice which is after submission redirect to a middle page named test.aspx which redirects to your destination page, so in this situation back button won't work.



回答4:

You can post the data from the textbox using AJAX, which is better since you will not have "Back" button.

For small stuff (one field, small info) I use this one:

$.post("savedata.aspx", { name: "John", time: "2pm" } );

Take a look at the others examples, it might be useful.

Just remember to reference jQuery in your project.



回答5:

Upon submit, you can save the fact that the form has been submitted in a session variable. When the form is submitted, you first test for this variable. If the variable is true (or whatever you set it to be) then do nothing. The user can refresh and nothing will be resubmitted.