验证CheckboxList

时间:2011-05-04 12:21:43

标签: c# asp.net checkbox

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

        <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>

3 个答案:

答案 0 :(得分:1)

我为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>

希望它对你有所帮助。

答案 1 :(得分:0)

使用可以使用CustomValidator控件。

答案 2 :(得分:0)

这个答案提供了一个非常简单和干净的解决方案,使用来自GitHubNuGet的酷库Dado.Validators

How to validate a user chose at least one checkbox?