
I had a requirement for such on a project I am doing and searching through the NET revealed few, and those that came up were not good at all. You may not find this any good, but I can tell you that it works, and that is good enough for me lol. Any bugs or hiccups you may find
OK so the class which inherits from the base validator is as follows:
using System;
using System.Data;
using System.Configuration;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace MyNamespace.CheckBoxListValidator
{
public class CheckBoxListValidator : BaseValidator
{
public Int16 MinimumNumberOfSelections
{
get
{
object o = ViewState["MinimumNumberOfSelections"];
if (o == null)
return 0;
return (Int16)ViewState["MinimumNumberOfSelections"];
}
set
{
ViewState["MinimumNumberOfSelections"] = value;
}
}
protected override bool ControlPropertiesValid()
{
return true;
}
protected override bool EvaluateIsValid()
{
CheckBoxList cbl = (CheckBoxList)FindControl(ControlToValidate);
if (cbl == null)
throw new Exception("cbl is null");
if (MinimumNumberOfSelections == 0)
{
foreach (ListItem li in cbl.Items)
{
if (li.Selected)
return true;
}
return false;
}
else
{
int noSelected = 0;
foreach (ListItem li in cbl.Items)
{
if (li.Selected)
noSelected++;
}
if (noSelected >= MinimumNumberOfSelections)
return true;
else
return false;
}
}
protected bool EvaluateIsChecked()
{
CheckBoxList _cbl = ((CheckBoxList)this.FindControl(this.ControlToValidate));
foreach (ListItem li in _cbl.Items)
{
if (li.Selected == true)
{
return true;
}
}
return false;
}
protected override void OnPreRender(EventArgs e)
{
if (this.EnableClientScript) { this.ClientScript(); }
base.OnPreRender(e);
}
protected void ClientScript()
{
this.Attributes["evaluationfunction"] = "cblv_clientVal";
StringBuilder validationScript = new StringBuilder();
if (MinimumNumberOfSelections == 0)
{
validationScript.AppendLine("function cblv_clientVal(val)");
validationScript.AppendLine("{");
validationScript.AppendFormat("var controlToValidate = document.getElementById('{0}').controltovalidate;", this.ClientID);
validationScript.AppendLine("var controlToValidate = document.getElementById(controlToValidate);");
validationScript.AppendLine("for(var i = 0; i < controlToValidate.getElementsByTagName(\"INPUT\").length;i++)");
validationScript.AppendLine("{");
validationScript.AppendLine(" var currentItem = controlToValidate.getElementsByTagName(\"INPUT\")[i];");
validationScript.AppendLine(" if(currentItem.type == \"checkbox\" && currentItem.checked)");
validationScript.AppendLine(" return true;");
validationScript.AppendLine("}");
validationScript.AppendLine("return false;");
validationScript.AppendLine("}");
}
else
{
validationScript.AppendLine("function cblv_clientVal(val)");
validationScript.AppendLine("{");
validationScript.AppendFormat("var controlToValidate = document.getElementById('{0}').controltovalidate;", this.ClientID);
validationScript.AppendLine();
validationScript.AppendLine("var controlToValidate = document.getElementById(controlToValidate);");
validationScript.AppendFormat("var currentCount = 0, targetCount = {0}", MinimumNumberOfSelections.ToString());
validationScript.AppendLine();
validationScript.AppendLine("for(var i = 0; i < controlToValidate.getElementsByTagName(\"INPUT\").length;i++)");
validationScript.AppendLine("{");
validationScript.AppendLine(" var currentItem = controlToValidate.getElementsByTagName(\"INPUT\")[i];");
validationScript.AppendLine(" if(currentItem.type == \"checkbox\" && currentItem.checked)");
validationScript.AppendLine(" {");
validationScript.AppendLine(" currentCount++;");
validationScript.AppendLine(" }");
validationScript.AppendLine("}");
validationScript.AppendLine(" if(currentCount >= targetCount)");
validationScript.AppendLine(" {");
validationScript.AppendLine(" return true;");
validationScript.AppendLine(" }else{");
validationScript.AppendLine(" return false;");
validationScript.AppendLine(" }");
validationScript.AppendLine("}");
}
this.Page.ClientScript.RegisterClientScriptBlock(typeof(string), "CheckBoxValidationScript", validationScript.ToString(), true);
}
}
}
To test this simply create a class in your App_Code folder and paste this in. To enable it so you can add it in, go into your web config in the system.web section and add the following. Called the TagPrefix whatever you want, make your the namespace ties in though
<pages>
<controls>
<add tagPrefix="aebs" namespace="MyNamespace.CheckBoxListValidator"/>
</controls>
</pages>
Finally below I have done two examples of it in action. One uses a property I have added called MinimumNumberOfSelections and the other doesn't. That simply means if you need the user to select more than one then this work.
With the MinimumNumberOfSelections included
<form id="form1" runat="server">
<div>
<aebs:CheckBoxListValidator ID="CheckBoxListVal1" runat="server" MinimumNumberOfSelections="2" ControlToValidate="CheckBoxList1" ErrorMessage="Please select at least one check item" Display="Dynamic" EnableClientScript="true"></aebs:CheckBoxListValidator>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Text="Value1" Value="Value1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="Value2"></asp:ListItem>
<asp:ListItem Text="Value3" Value="Value3"></asp:ListItem>
<asp:ListItem Text="Value4" Value="Value4"></asp:ListItem>
<asp:ListItem Text="Value5" Value="Value5"></asp:ListItem>
<asp:ListItem Text="Value6" Value="Value6"></asp:ListItem>
<asp:ListItem Text="Value7" Value="Value7"></asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
WithOUT the MinimumNumberOfSelections included
<form id="form1" runat="server">
<div>
<aebs:CheckBoxListValidator ID="CheckBoxListVal1" runat="server" ControlToValidate="CheckBoxList1" ErrorMessage="Please select at least one check item" Display="Dynamic" EnableClientScript="true"></aebs:CheckBoxListValidator>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Text="Value1" Value="Value1"></asp:ListItem>
<asp:ListItem Text="Value2" Value="Value2"></asp:ListItem>
<asp:ListItem Text="Value3" Value="Value3"></asp:ListItem>
<asp:ListItem Text="Value4" Value="Value4"></asp:ListItem>
<asp:ListItem Text="Value5" Value="Value5"></asp:ListItem>
<asp:ListItem Text="Value6" Value="Value6"></asp:ListItem>
<asp:ListItem Text="Value7" Value="Value7"></asp:ListItem>
</asp:CheckBoxList>
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
No comments:
Post a Comment