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>

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

7 Comments

I tried putting validation on the checkbox and I got an error message saying the the ControlToValidate was invalid for a checkbox.
what type of validator did you use - I don't see it in the code you posted
i am using all kinds, Compare, Requried, RegularExpression
if you mean the check box, i tried to RequiredValidator on it
I am surpised a required validator didn't work - try a custom validator with javascript to test the checkbox
|
2

Asp.net is stupid because when a control is disabled, there's all sorts of stuff that won't happen with it.

In this case, the js code to cause client side validation will not be created for a disabled button.

If you try to manually add in the clientside js code from the code behind,

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    registerButton.OnClientClick = String.Format("javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions('{0}', '', true, '', '', false, false))", registerButton.UniqueID)
End Sub

It will not work because asp.net will not render all the attributes for a disabled control.

There are two solutions then. Either disable the control with JS at client side or add the javascript to perform a postback with validation at client side.

What I did for my program was to add code to create js code to disable the button at the client side:

    If btnSubmit.Enabled = False Then
        'Asp.net will not render all the attributes for a control that is disabled.  Even if you
        'try to set the OnClientClick attribute manually in code behind for the control, it will 
        'not work.  Asp.net will not render the attribute.  This is a workaround for this problem.
        btnSubmit.Enabled = True

        strJavaScript &= "btnSubmit.disabled = true;"

    End If

    ClientScript.RegisterStartupScript(Page.GetType(), "myStartupScript", strJavaScript, True)

Note that btnSubmit is a javascript variable that is already defined in my client side code.

Comments

0

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

4 Comments

The client-side works just fine. I don't need a trip to the server just to enable a button.
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
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.
I ended up just doing it during a postback with a bit of ajax.

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.