0

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?

2
  • Try OnClientClick="this.disabled=true; return true;" encosia.com/disable-a-button-control-during-postback Commented May 5, 2015 at 20:00
  • Thank you for the link. It led me to add UseSubmitBehavior="False" to my button, which did the trick. Commented Jun 18, 2015 at 19:38

2 Answers 2

0

Forgive me if I have misunderstood the question. I would just do it directly on the submit button with javascript. I had a similar problem, where I had to manipulate the site with JS before posting to the server. This snippet uses JQuery.

http://jsfiddle.net/sckyq7ta/4/

$("[type='submit']").click(function (event, args) {
if (!args) {
    event.preventDefault();

    //Do your thing
    $(this).attr("disabled", "true");
    $(this).html("Please wait..");

    //call myself with args to enable default (posting to server)
    $(this).trigger("click", {
        enableDefault: true
    });
}
});
Sign up to request clarification or add additional context in comments.

Comments

0

Thank you for the reply. The link in the comment above led me to add UseSubmitBehavior="False" to my button and that was what I needed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.