asp.net中的复选框自定义验证器

dee*_*pti 9 asp.net checkbox customvalidator

我使用了自定义验证器

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

这是服务器验证的代码..我已经写过检查是否已检查.我已控制验证为复选框

<asp:CustomValidator runat="server" ErrorMessage="Please Select Status" 
            ID="cvIsActive" Font-Size="Smaller" 
            onservervalidate="cvIsActive_ServerValidate" ControlToValidate="chkIsActive"></asp:CustomValidator>
Run Code Online (Sandbox Code Playgroud)

但是一旦页面加载就会给出错误

Control 'chkIsActive' referenced by the ControlToValidate property of 'cvIsActive' cannot be validated. 
Run Code Online (Sandbox Code Playgroud)

gbs*_*gbs 10

与CheckBox一起使用时,您不需要为CustomValidator设置ControlToValidate属性,只需在服务器验证中使用它:

 args.IsValid = chkIsActive.Checked;
Run Code Online (Sandbox Code Playgroud)