So enough with the talk lets take a look at the code.
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyValidations
{
[DefaultProperty("ErrorMessage")]
public class RequiredSelectedItemValidator : System.Web.UI.BaseValidator
{
private ListControl _listctrl;
protected override bool ControlPropertiesValid()
{
Control ctrl = FindControl(ControlToValidate);
If (ctrl != null)
_listctrl = ctrl as ListControl;
return (_listctrl != null);
else
return false; // raise exception
}
protected override bool EvaluateIsValid()
{
return (_listctrl is DropDownList) ? (_lisctrl.SelectedIndex != 0) :
( _lisctrl.SelectedIndex != -1);
}
}
}
|