开发者

Validation for CheckboxList

Can you tell how can i implement validation in my checkboxlist. Validation must check if any chechbox cheked, if not, valida开发者_运维知识库tion error must be happened. This is my code:

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


I have created custom class for checkBoxList validation:

Here is my custom class code.

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

You can register custom class on page like as

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

After that you can set validation as usual

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

Hope it will help for you.


Use can use CustomValidator control.


This answer provides a very very simple and clean solution using a cool library, Dado.Validators, from GitHub or NuGet

How to validate a user chose at least one checkbox?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜