views:

74

answers:

2

I have an aspx page that contains a checkbox, and a button. The button is disabled by default until the user checks the checkbox. It looks like when I add the attribute enabled="false" to the button, it removes the validation. When the button is enabled, I still want the validation to work. Here is the code and markup for the different parts.

Checkbox:

<asp:CheckBox runat="server" ID="termsCheckBox" />
<label style="display: inline;">
I agree to the Terms & Conditions</label>

Button:

<asp:Button runat="server" ID="registerButton" OnClick="registerButton_Click1" 
Text="Register" Enabled="false" ValidationGroup="accountValidation" />

JQuery:

<script type="text/javascript" language="javascript">
$(document).ready(function () {
    $('#<%=termsCheckBox.ClientID %>').click(function () {
        var checked = $(this).val();

        if (checked != undefined)
            $('#<%=registerButton.ClientID %>').removeAttr('disabled');
        else
            $('#<%=registerButton.ClientID %>').attr('disabled', 'disabled');
    });
});
</script>
+2  A: 

It seems like the validation should be attached to the checkbox, not the button.

Ray
I tried putting validation on the checkbox and I got an error messagesaying the the ControlToValidate was invalid for a checkbox.
Xaisoft
what type of validator did you use - I don't see it in the code you posted
Ray
i am using all kinds, Compare, Requried, RegularExpression
Xaisoft
if you mean the check box, i tried to RequiredValidator on it
Xaisoft
I am surpised a required validator didn't work - try a custom validator with javascript to test the checkbox
Ray
one of my original questions is why does disabling a button disable the validation anyway. Shouldn't the validation be reenabled when the button is enabled or is it because i am handing it on the client side and the validation is not being added.
Xaisoft
I don't think validating a button makes any sense - there is nothing to test. Validation is meant to test values in data entry controls (checkboxes, text boxes, etc) - a button does not have a value which can be entered.modified by the user. So my guess is that you have run into an unsupported situation where the behavior is undefined.
Ray
A: 

I think it would work if you enabled/disabled the button on the checkBox server-side event rather than client side since the associated client-side validation code will be generated to run for ValidationGroup of the button

Sunny
The client-side works just fine. I don't need a trip to the server just to enable a button.
Xaisoft
I mean for the asp.net button to be part of the ValidationGroup validation performed by the .Net framework, you will need to do a postback to the server to ensure that the script needed to have the button attach to a handler on the client. If you have custom script on the client, then you would need to ensure that it has been attached to the client click event of the button to be fired... unless I missed something
Sunny
Ok, i think i got you correctly. Are you saying that because I am handing it client-side and not doing a post back, the validation for the button is not being handled because it the script for it is not being added when it is enabled.
Xaisoft
yep, I think that is the reason why...
Sunny
let me try a few things.
Xaisoft
I ended up just doing it during a postback with a bit of ajax.
Xaisoft