Objective
This article has two goals.
- Implement a good ListValidator to validate whether at least one item in a
CheckBoxList
or RadioButtonList
has been checked.
- Demonstrate a fully functional validator.
Common omissions / problems
Many validators seem to fall down in one area or another. This control is an attempt to demonstrate how a complete validator should look. As this is my first article, I'm sure I'll miss something or lots of things. Please let me know and I'll try to keep it up-to-date. Here are a few common problems. My example shows how to implement these features.
- Validators that don't implement client script.
- Validators that don't implement
EnableClientScirpt="false"
.
- Validator client scripts that don't work with multiple validators on a page.
Here's the code
using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace CompanyName.Web.Controls.Validators
{
[DefaultProperty("ErrorMessage")]
public class ListControlRequiredFieldValidator : BaseValidator
{
protected override bool ControlPropertiesValid()
{
Control controlToValidate =
FindControl(ControlToValidate) as ListControl;
return (controlToValidate != null);
}
protected override bool EvaluateIsValid()
{
return this.EvaluateIsChecked();
}
protected bool EvaluateIsChecked()
{
ListControl listToValidate =
((ListControl) this.FindControl(this.ControlToValidate));
foreach( ListItem li in listToValidate.Items )
{
if ( li.Selected == true )
return true;
}
return false;
}
protected override void OnPreRender( EventArgs e )
{
System.Web.HttpContext.Current.Trace.Write(
"Override OnPreRender");
if(this.DetermineRenderUplevel() && this.EnableClientScript)
{
Page.ClientScript.RegisterExpandoAttribute(this.ClientID,
"evaluationfunction", "ListItemVerify");
Page.ClientScript.RegisterExpandoAttribute(this.ClientID,
"minimumNumberOfSelectedCheckBoxes", "1");
this.RegisterClientScript();
}
else
{
this.Attributes.Remove("evaluationfunction");
}
base.OnPreRender( e );
}
protected void RegisterClientScript()
{
string script = @"
<script language=""javascript"">
function ListItemVerify(val)
{
var control =
document.getElementById(val.controltovalidate);
var minimumNumberOfSelectedCheckBoxes =
parseInt(val.minimumNumberOfSelectedCheckBoxes);
var selectedItemCount = 0;
var liIndex = 0;
var currentListItem =
document.getElementById(control.id +
'_' + liIndex.toString());
while (currentListItem != null)
{
if (currentListItem.checked) selectedItemCount++;
liIndex++;
currentListItem =
document.getElementById(control.id +
'_' + liIndex.toString());
}
return selectedItemCount >=
minimumNumberOfSelectedCheckBoxes;
}
</script>
";
this.Page.ClientScript.RegisterClientScriptBlock(
typeof(ListControlRequiredFieldValidator),
"ListRequiredValidator_Script",script);
}
}
}
Example usage
Register the library.
<%@ Register TagPrefix="CompanyName"
NameSpace="CompanyName.Web.Controls.Validators"
Assembly="CompanyName.Web" %>
Create the validator. Imagine that the CheckBoxList
you wish to validate is called MyCheckList
.
<CompanyName:ListControlRequiredFieldValidator
ControlToValidate="MyCheckList"
display="Dynamic"
ErrorMessage="Select at least one item"
EnableClientScript="true"
runat="Server">
Tick at least one box
</CompanyName:ListControlRequiredFieldValidator>
History
- 12 April, 2005 -- Original version posted
- 17 February, 2006 -- Updated
- 24 July, 2007 -- Updated