开发者

Not allow resubmit of page

开发者 https://www.devze.com 2023-02-23 01:30 出处:网络
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 开发者_Go百科to be able to resubmit that data. In

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 开发者_Go百科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?


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


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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消