Thursday 26 May 2016

Prevent double clicking or multiple Form submission after the ASP.Net Button is clicked?

Some time it happens that we write a complex code or a lengthy DB operation with in a button submit event which take more than a second or two to complete it operation and control comes back to the button. In these scenario the user has chance to click the button again and there are high chance that the code with in the button get executed again. Though this can be handled in server side, it is better to handle this on client side. Out of many way to handle, I found the following was as more robust.

Attach an client click event to the button

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClientClick="return preventReSubmissions();" OnClick="btnSubmit_Click" />

Create a javascript method preventReSubmissions() with the below content.

<script type="text/javascript" src="jquery/1.11.2/jquery.min.js"></script> <script type="text/javascript"> var isSubmitted = false; function preventReSubmissions() { if (!isSubmitted) { $('#<%=btnSubmit.ClientID %>').val('Plz Wait..'); isSubmitted = true; return true; } else { return false; } } </script>

No comments:

Post a Comment