Xaisoft Xaisoft - 1 year ago 114
jQuery Question

Disabled asp.net button disables validation for button

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.


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


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


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

if (checked != undefined)
$('#<%=registerButton.ClientID %>').removeAttr('disabled');
$('#<%=registerButton.ClientID %>').attr('disabled', 'disabled');

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download