using System; using System.Text; using System.Web.UI; using System.Web.UI.WebControls; namespace Custom.Validators { 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(""); 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; } } }