CheckBoxList的RequiredValidator

CheckBoxList的RequiredValidator

前幾天為了解決CheckBoxList的必填,捨棄我之前的作法(我之前是利用Javascript的方式),上網找了一些解法。

我們要去繼承BaseValidator,並且去覆寫他的ControlPropertiesValid屬性,因為如果是一般的RequiredValidator的話,當指定ControltoValidator給CheckBoxList時,此時似乎沒有發生作用@@。

說的再多也沒有讓大家看程式重要,這邊我新增一個專案,命名為CheckBoxRequireControl,詳細的程式碼如下

 


using System;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CheckBoxRequireControl
{
    public class RequiredFieldValidatorForCheckBoxLists : 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;
        }
    }
}

 

接著我將其Compiler之後,加到我左邊的工具箱

checkbox

之後就可以享受CheckBoxList的必選了

我做了一個測試頁,上面放1個CheckBoxList與1個Button

 


<asp:CheckBoxList ID="CheckBoxList1" runat="server">
        <asp:ListItem Text="test001" Value="01"></asp:ListItem>
        <asp:ListItem Text="test002" Value="02"></asp:ListItem>
        <asp:ListItem Text="test003" Value="03"></asp:ListItem>
    </asp:CheckBoxList>
    <cc1:RequiredFieldValidatorForCheckBoxLists ID="RequiredFieldValidatorForCheckBoxLists1" ControlToValidate="CheckBoxList1"
     Display="Dynamic" ErrorMessage="Must Be Choose"
        runat="server">
        
    </cc1:RequiredFieldValidatorForCheckBoxLists>
    <asp:Button ID="Button1" runat="server" Text="Button" />

畫面大概是像醬子

before after