Prevent Multiple Submissions of Forms in Asp.Net
Here is the issue. You have a form and someone double clicks the Submit button, and curiously enough you have two forms that were submitted! My first thought was to disable the button on click with javascript, and then when the page loaded again the button would be enabled, but that was a no go. Asp.Net does not fire events for disabled buttons.
Went looking around and found a page that suggests using a variable to check to see if it was submitted or not in javascript. As far as I can tell this is the best way of preventing this issue. If they have javascript disabled however then it might be a problem though, but I am not even going to worry about those < 1% individuals.
I changed their javascript function to not use the .Net markup inside the javascript to get the clientID. I prefer making the ClientIDMode="Static" and having just pure javascript in the function.
var isSubmitted = false;
function preventMultipleSubmissions() {
if (!isSubmitted) {
$('#btnSaveRecord').val('Submitting');
isSubmitted = true;
return true;
}
else {
return false;
}
}
<asp:Button ID="btnSaveRecord" class="btn btn-default" runat="server" Text="Add" OnClick="btnSaveRecord_Click" ClientIDMode="Static" OnClientClick="return preventMultipleSubmissions()"/>
http://www.aspsnippets.com/Articles/Disable-ASPNet-button-after-click-to-prevent-double-clicking.aspx
Went looking around and found a page that suggests using a variable to check to see if it was submitted or not in javascript. As far as I can tell this is the best way of preventing this issue. If they have javascript disabled however then it might be a problem though, but I am not even going to worry about those < 1% individuals.
I changed their javascript function to not use the .Net markup inside the javascript to get the clientID. I prefer making the ClientIDMode="Static" and having just pure javascript in the function.
var isSubmitted = false;
function preventMultipleSubmissions() {
if (!isSubmitted) {
$('#btnSaveRecord').val('Submitting');
isSubmitted = true;
return true;
}
else {
return false;
}
}
<asp:Button ID="btnSaveRecord" class="btn btn-default" runat="server" Text="Add" OnClick="btnSaveRecord_Click" ClientIDMode="Static" OnClientClick="return preventMultipleSubmissions()"/>
http://www.aspsnippets.com/Articles/Disable-ASPNet-button-after-click-to-prevent-double-clicking.aspx
Comments
Post a Comment