ASP.NET 4.0 Due to a network delay allowing users to hit the form submit button many times, resulting in duplicate entries, I’m trying to use Javascript to disable the button after the first click until postback. I do not know Javascript so I apologize for my cluelessness. The problem button is in a form within an updatepanel within a tabcontrol on a page with several forms, etc. so I’m using a validation group that contains different validators that turn off or on depending on a dropdown selection.
Here’s my button:
<asp:Button ID="BtnAddProd" runat="server" OnClick="BtnAddProd_Click"
Text="Add Status" ValidationGroup="Activity_ValControls"
onclientclick="disableBtnOnClick(this,'Activity_ValControls')"
CausesValidation="False" />
Here’s my Javascript:
function disableBtnOnClick(btnName, valGroup) {
var isGroupValid = Page_ClientValidate(valGroup);
//check if validation group is successful
if (isGroupValid) {
//set button to disabled so you can't click on it.
btnName.disabled = true;
btnName.value = 'Please wait...'
}
else {
alert('Page is not valid');//for testing
}
}
I’ve gone down many different roads and this is the closest I’ve gotten. It works in one case – only if the first click results in a validation error, then I correct the error and submit. If there are no errors, either due to my dropdown selection having no associated validators (i.e. when they want to add a free text comment record) or if I correctly enter all the validated fields the first time then it very quickly flashes to disabled with the ‘Please wait…’ text then back to enabled and default text. If I then click it many times very quickly, it eventually disables with the ‘Please wait…’ text and stays there.
Any ideas?