Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom validator for checkbox in asp.net

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. 
like image 579
deepti Avatar asked Mar 28 '11 17:03

deepti


1 Answers

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;
like image 115
gbs Avatar answered Sep 29 '22 10:09

gbs