ASP.Net: Prevent users from submitting a form more than once (disable submit button)

I found an easy way to prevent users from submitting a form more than once. Here is the code
btnSubmit.Attributes.Add("onclick", "this.disabled=true;" +
  ClientScript.GetPostBackEventReference(btnSubmit, "").ToString());
This will disable the button when the submit button is clicked, and it will automatically enable itself after the page has been postback.

1.The ClientScript.GetPostBackEventReference(btnSubmit, "") will generate the __doPostBack('btnSubmit', ''); for the btnSubmit button.
2.If you use the UpdatePanel control, make sure you update the the submit button as well.
3.It is not 100% working for all situations, so be sure to test it before applying on your web application.

No comments:

Post a Comment