2

I'm using the following code to disable my button when it is clicked:

OnClientClick="this.disabled = true" UseSubmitBehavior="false"

The problem is that the page has several asp.net validation controls. So when I click the button and the button disables. If any of the validation controls has failed, the button stays disabled (and never gets enabled - because a postback is never executed).

I doubt there is a .net way to solve this, so I hope there is some kind of javascript that can be inserted in the OnClientClick-property. Hopefully I can check if the validation controls have returned any error, before i disable the button...

Iswanto San
  • 18,263
  • 13
  • 58
  • 79
baddaydaddy
  • 604
  • 8
  • 25

3 Answers3

1

Trigger the validation on your own, and use the validation to determine whether the button should stay disabled.

Your button (note that OnClientClick is returning true/false):

<asp:Button ID="Button1" runat="server" Text="Go" OnClientClick="return validatePage(this);" ... />

Your JavaScript function:

validatePage = function(button)
{
    var isValid = Page_ClientValidate();
    button.disabled = isValid;

    //return validation - if true then postback
    return isValid;
}
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • That looks very fine... exactly what I was looking for. Unfortunately the Page_ClientValidate() returns false on every call (when controls are invalid, when they are valid and even when they don't have anything to validate). And off course the serverside event is never executed. – baddaydaddy Sep 13 '11 at 06:43
0

The onsubmit event on form1 shos how to do it in a simple way. Try this:

<form id="form1" runat="server" onsubmit="setTimeout(function() {form1.btn1.value = 'Wait...'; form1.btn1.disabled = true; }, 0);">
<div>
    <asp:Label id="lbl1" runat="server" Text="Simple test:" AssociatedControlID="text1" />
    <asp:TextBox runat="server" ID="text1" />
    <asp:RequiredFieldValidator ErrorMessage="Fill the TextBox!" ControlToValidate="text1" runat="server" />
    <asp:Button Text="text" runat="server" ID="btn1" OnClick="btn1_Click" />
</div>
</form>
Marquinho Peli
  • 4,795
  • 4
  • 24
  • 22
0

This question has been asked before. Here are just a few answers I found that may help you:

JQuery validation plugin re-enable submit after validation

Re-Enable button after submit

Use jQuery to Enable/Disable a Button based on vars

Community
  • 1
  • 1
Kiley Naro
  • 1,749
  • 1
  • 14
  • 20
  • I'm sorry, but none of the posts made any sense to me... I'm not familiar with either JQuery or php, and I definitely don't want to rewrite my asp.net validation controls... – baddaydaddy Sep 13 '11 at 08:48
  • Sounds like great motivation to learn! jQuery is unbelievably useful and won't be going away any time soon. I would strongly recommend becoming at least somewhat familiar with it not only because you're going to see it everywhere, but because it's incredibly useful. If you can edit your question to provide more sample code, we might be able to help you better develop a working solution. – Kiley Naro Sep 13 '11 at 15:38
  • My problem is quite simple, and I can't see why more code samples will provide any more specification about it. Because both control validation and disabling of the button is done clientside (but individually), the button is disabled although the page is not valid. Therefor the page will not make a postback - and the button will never be enabled. I know about JQuery although I don't know it enough to replace my asp.net validation controls. It will take too long to re-program. – baddaydaddy Sep 14 '11 at 06:51