Just a quick how-to for anyone wanting to add a conditional check box on an ASP.NET Webform.
You need a checkbox with a prompt. There is a custom validator with a client and server side validation method specified.
<p>
* By ticking the box below you confirm that you are authorised to do so on behalf of your business & you agree to our terms & conditions (set out above).
<div>
<asp:CheckBox runat="server" ID="AcceptTerms" name="AcceptTerms" ClientIDMode="Static"/> I accept the terms & conditions.
</div>
<asp:CustomValidator runat="server" ID="CheckBoxRequired" EnableClientScript="true" SetFocusOnError="true"
CssClass="field-validation-error" Display="Dynamic" OnServerValidate="AcceptTermsValidation"
Text="You must accept the terms & conditions before continuing." ClientValidationFunction="AcceptTermsValidate"
ValidationGroup="MainValidationGroup" />
</p>
<asp:Button id="Button2" Text="Accept Terms and Create Account" OnClick="ContinueBtn_OnClick"
OnClientClick="return showLoader(this);" runat="server" CssClass="input-form-button"
ValidationGroup="MainValidationGroup" />
Code for the javascript validator.
function AcceptTermsValidate(source, args) {
args.IsValid = $("#AcceptTerms").is(':checked');
}
Code for the server side validator.
protected void AcceptTermsValidation(object sender, ServerValidateEventArgs args)
{
args.IsValid = AcceptTerms.Checked;
}
If the user does not tick the checkbox and clicks on the button the custom validator Text message will be displayed.

You must be logged in to post a comment.