9

i have used custom validator

 protected void cvIsActive_ServerValidate(object source,ServerValidateEventArgs args)
        {
            if(args.Value.Length==1)
                args.IsValid = true;
            else
                args.IsValid = false;
        }       

this is code for server validate.. i have written to check whether its checked or not. i have taken control to validate as checkbox

<asp:CustomValidator runat="server" ErrorMessage="Please Select Status" 
            ID="cvIsActive" Font-Size="Smaller" 
            onservervalidate="cvIsActive_ServerValidate" ControlToValidate="chkIsActive"></asp:CustomValidator>

but as soon as page loads it giving error

Control 'chkIsActive' referenced by the ControlToValidate property of 'cvIsActive' cannot be validated. 
1
  • 3
    A more robust answer can be found here stackoverflow.com/q/1228112/1066291. They are using it to make the checkbox required but you could write any custom validation logic. Commented Jan 26, 2012 at 19:11

1 Answer 1

10

You don't need to set ControlToValidate property for CustomValidator when using it with CheckBox and simply use this in Server Validate like:

 args.IsValid = chkIsActive.Checked;
Sign up to request clarification or add additional context in comments.

Comments

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.