CheckBoxList require validator

воскресенье, 14 сентября 2008 г. · 0 коммент.

I've needed to validate Check box list against whether something is checked or not. It was difficult to find working solution so i've decided to create my own validator. I've tested it on FireFox, IE and Opera. It looks like it works properly.

using System;

using System.Web.UI.WebControls;

namespace DragonSoft.RestForYou.BusinessLayer.Controls
{
/// <summary>
/// Checks where one or more items are selected on for check box list
/// </summary>
public class CheckBoxListValidator : BaseValidator
{
#region Constants

private const string ValidationScriptCode = @"
function CheckBoxValidatorIsValid(val)
{
var controlObj = document.getElementById(val.controltovalidate);

if (!controlObj)
return false;

var checkBoxList = document.getElementsByTagName('INPUT');

for (var i = 0; i < checkBoxList.length; i++)
{
if (checkBoxList[i].type != 'checkbox')
continue;

if (!IsChildOf(checkBoxList[i], controlObj))
continue;

if (checkBoxList[i].checked)
return true;
}
return false;
}


function IsChildOf(childNode, parentNode)
{
var currentParent = childNode.parentNode;

while (currentParent)
{
if (currentParent.id == parentNode.id)
return true;
currentParent = currentParent.parentNode;
}

return false;
}
"
;

private const string ValidationScriptKey = "CheckBoxValidationScript";

#endregion

#region Overriden methods

/// <summary>
/// Determines whether the control specified by the <see cref="P:System.Web.UI.WebControls.BaseValidator.ControlToValidate"/> property is a valid control.
/// </summary>
/// <returns>
/// true if the control specified by <see cref="P:System.Web.UI.WebControls.BaseValidator.ControlToValidate"/> is a valid control; otherwise, false.
/// </returns>
/// <exception cref="T:System.Web.HttpException">No value is specified for the <see cref="P:System.Web.UI.WebControls.BaseValidator.ControlToValidate"/> property.- or -The input control specified by the <see cref="P:System.Web.UI.WebControls.BaseValidator.ControlToValidate"/> property is not found on the page.- or -The input control specified by the <see cref="P:System.Web.UI.WebControls.BaseValidator.ControlToValidate"/> property does not have a <see cref="T:System.Web.UI.ValidationPropertyAttribute"/> attribute associated with it; therefore, it cannot be validated with a validation control.</exception>
protected override bool ControlPropertiesValid()
{
return FindControl(ControlToValidate) is CheckBoxList;
}


/// <summary>
/// When overridden in a derived class, this method contains the code to determine whether the value in the input control is valid.
/// </summary>
/// <returns>
/// true if the value in the input control is valid; otherwise, false.
/// </returns>
protected override bool EvaluateIsValid()
{
return EvaluateIsChecked();
}

/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
/// </summary>
/// <param name="e">A <see cref="T:System.EventArgs"/> that contains the event data.</param>
protected override void OnPreRender(EventArgs e)
{
if (EnableClientScript)
RegisterClientScript();

base.OnPreRender(e);
}

#endregion

#region Helper methods

/// <summary>
/// Evaluates the is checked.
/// </summary>
private bool EvaluateIsChecked()
{
foreach (ListItem listItem in ((CheckBoxList)FindControl(ControlToValidate)).Items)
{
if (listItem.Selected)
return true;
}
return false;
}

/// <summary>
/// Registers the client script.
/// </summary>
private void RegisterClientScript()
{
Page.ClientScript.RegisterExpandoAttribute(
ClientID,
"evaluationfunction",
"CheckBoxValidatorIsValid"
);

if (!Page.ClientScript.IsClientScriptBlockRegistered(typeof(CheckBoxListValidator), ValidationScriptKey))
Page.ClientScript.RegisterClientScriptBlock(
typeof(CheckBoxListValidator),
ValidationScriptKey,
ValidationScriptCode,
true);
}


#endregion
}
}

Обо мне

Моя фотография
Кто к нам с чем и зачем, тот от того и того!