Validation for CheckboxList

本文关键字:CheckboxList for Validation | 更新日期: 2023-09-27 17:54:32

你能告诉我如何在我的复选框列表中实现验证吗?验证必须检查是否有任何复选框被选中,如果没有,则必须发生验证错误。这是我的代码:

        <asp:CheckBoxList runat="server" ID="chRoles">
        <asp:ListItem Text="role1" Value="role1" />
        <asp:ListItem Text="role2" Value="role2" />
        <asp:ListItem Text="role3" Value="role3" />
        <asp:ListItem Text="role4" Value="role4" />
        <asp:ListItem Text="role5" Value="role5" />
        <asp:ListItem Text="role6" Value="role6" />
        </asp:CheckBoxList>

Validation for CheckboxList

我已经创建了checkBoxList验证的自定义类:

这是我的自定义类代码。

namespace Custom.Validators
{
    public class RFVCBoxList : BaseValidator
    {
        private const string SCRIPTBLOCK = "RFV4CL";
        protected override bool ControlPropertiesValid()
        {
            Control ctrl = FindControl(ControlToValidate);
            if (ctrl != null)
            {
                CheckBoxList _listctrl = (CheckBoxList)ctrl;
                return (_listctrl != null);
            }
            else
                return false;
        }
        protected override bool EvaluateIsValid()
        {
            return EvaluateIsChecked();
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (EnableClientScript) { this.ClientScript(); }
        }
        private void ClientScript()
        {
            StringBuilder sb_Script = new StringBuilder();
            sb_Script.Append("<script language='"javascript'">");
            sb_Script.Append("'r");
            sb_Script.Append("'r");
            sb_Script.Append("function cb_verify(sender) {");
            sb_Script.Append("'r");
            sb_Script.Append("var val = document.getElementById(document.getElementById(sender.id).controltovalidate);");
            sb_Script.Append("'r");
            sb_Script.Append("var col = val.getElementsByTagName('"*'");");
            sb_Script.Append("'r");
            sb_Script.Append("if ( col != null ) {");
            sb_Script.Append("'r");
            sb_Script.Append("for ( i = 0; i < col.length; i++ ) {");
            sb_Script.Append("'r");
            sb_Script.Append("if (col.item(i).tagName == '"INPUT'") {");
            sb_Script.Append("'r");
            sb_Script.Append("if ( col.item(i).checked ) {");
            sb_Script.Append("'r");
            sb_Script.Append("'r");
            sb_Script.Append("return true;");
            sb_Script.Append("'r");
            sb_Script.Append("}");
            sb_Script.Append("'r");
            sb_Script.Append("}");
            sb_Script.Append("'r");
            sb_Script.Append("}");
            sb_Script.Append("'r");
            sb_Script.Append("'r");
            sb_Script.Append("'r");
            sb_Script.Append("return false;");
            sb_Script.Append("'r");
            sb_Script.Append("}");
            sb_Script.Append("'r");
            sb_Script.Append("}");
            sb_Script.Append("'r");
            sb_Script.Append("</script>");
            Page.ClientScript.RegisterClientScriptBlock(GetType(), SCRIPTBLOCK, sb_Script.ToString());
            Page.ClientScript.RegisterExpandoAttribute(ClientID, "evaluationfunction", "cb_verify");
        }

        private bool EvaluateIsChecked()
        {
            CheckBoxList _cbl = ((CheckBoxList)FindControl(ControlToValidate));
            foreach (ListItem li in _cbl.Items)
            {
                if (li.Selected)
                {
                    return true;
                }
            }
            return false;
        }
        public RFVCBoxList()
        {
            //
            // TODO: Add constructor logic here
            //
        }
    }
}

您可以在页面上注册自定义类,如

<%@ Register TagPrefix="CC1" Namespace="Custom.Validators" %>

之后你可以像往常一样设置验证

<CC1:RFVCBoxList ID="rfvContactType" runat="server" ControlToValidate="chkContactType"
Display="None" ErrorMessage="Please select contact type" SetFocusOnError="True"
ValidationGroup="Photographer"></CC1:RFVCBoxList>

希望对你有帮助

可以使用CustomValidator控件

这个答案提供了一个非常非常简单和干净的解决方案,使用一个很酷的库,Dado。验证器,来自GitHub或NuGet

如何验证用户选择了至少一个复选框?