ASP.NET – Create Validators for CheckBoxLists

in .NET Development, ASP.NET, C#

To validate ASP.NET CheckBoxLists, you need to write an own validator. A CustomValidator will not work, because it can’t take a CheckBoxList as a single object in the ControlToValidate property. ASP.NET will generate sequential IDs for the CheckBoxes in a CheckBoxList object, so there is no chance to validate it as “single control”. That works for non-listing objects, like textfields, datepickers etc., but not for list controls.

Here you find three different validators, each has its own purpose:

  • RequiredMinimumOneValidatorForCheckBoxList. This validator is used to check, if  – in minimum – one checkbox is checked.
  • RequiredMaximumOneValidatorForCheckBoxList. This validator can be used to test, if only one checkbox is checked (either…or).
  • RequiredAllValidatorForCheckBoxList. This validator tests, if all checkboxes are checked in the checkboxlist

Here is the code:

RequiredMinimumOneValidatorForCheckBoxList


public class RequiredMinimumOneValidatorForCheckBoxList : BaseValidator
{
private ListControl _listControl;

/// <summary>
/// Constructor
/// </summary>
public RequiredMinimumOneValidatorForCheckBoxList()
{
base.EnableClientScript = false;
}

/// <summary>
/// Checks, if we can find the control to validate
/// </summary>
/// <returns></returns>
protected override bool ControlPropertiesValid()
{
Control ctrl = FindControl(ControlToValidate);
if (ctrl != null)
{
_listControl = (ListControl)ctrl;
return (_listControl != null);
}

return false;
}

/// <summary>
/// Checks, if in minimum one checkbox has been checked by the user
/// </summary>
/// <returns></returns>
protected override bool EvaluateIsValid()
{
return _listControl.SelectedIndex != -1;
}
}

RequiredMaximumOneValidatorForCheckBoxList

public class RequiredMaximumOneValidatorForCheckBoxList : BaseValidator
{
private ListControl _listControl;

/// <summary>
/// Constructor
/// </summary>
public RequiredMaximumOneValidatorForCheckBoxList()
{
base.EnableClientScript = false;
}

/// <summary>
/// Checks, if we can find the control to validate
/// </summary>
/// <returns></returns>
protected override bool ControlPropertiesValid()
{
Control ctrl = FindControl(ControlToValidate);
if (ctrl != null)
{
_listControl = (ListControl)ctrl;
return (_listControl != null);
}

return false;
}

/// <summary>
/// Checks, if only one checkbox has been checked by the user
/// </summary>
/// <returns></returns>
protected override bool EvaluateIsValid()
{
int intSelCount = 0;
foreach (ListItem item in _listControl.Items)
{
if (item.Selected)
{
intSelCount++;
if (intSelCount > 1)
{
return false;
}
}
}
return true;
}
}

RequiredAllValidatorForCheckBoxList


public class RequiredAllValidatorForCheckBoxList : BaseValidator
{
private ListControl _listControl;

/// <summary>
/// Constructor
/// </summary>
public RequiredAllValidatorForCheckBoxList()
{
base.EnableClientScript = false;
}

/// <summary>
/// Checks, if we can find the control to validate
/// </summary>
/// <returns></returns>
protected override bool ControlPropertiesValid()
{
Control ctrl = FindControl(ControlToValidate);
if (ctrl != null)
{
_listControl = (ListControl)ctrl;
return (_listControl != null);
}

return false;
}

/// <summary>
/// Checks, if all checkboxes have been checked by the user
/// </summary>
/// <returns></returns>
protected override bool EvaluateIsValid()
{
foreach (ListItem item in _listControl.Items)
{
if (!item.Selected)
{
return false;
}
}
return true;
}
}

For a short Test, add this code to the your page or user control codebehind:

protected override void OnInit(EventArgs e)
{
base.OnInit(e);

RequiredMaximumOneValidatorForCheckBoxList validator = new RequiredMaximumOneValidatorForCheckBoxList();
validator.ControlToValidate = "ID of your CheckBoxList";
validator.ErrorMessage = "The error message, that will be shown in case of a validation error";
Controls.Add(validator);
}
0 Comments

Leave a Reply

Using Gravatars in the comments - get your own and be recognized!

XHTML: These are some of the tags you can use: <a href=""> <b> <blockquote> <code> <em> <i> <strike> <strong>