As you all know the ASP.NET
RequiredFieldValidator control checks that the validated control
to see if it contains a
value. If it doesn't it displays an
error message. The problem is that
does not work
) such as
actually it throws an exception in the
function if you try to set the
property of the
control to one of the above controls. So
I decided to write a server side validation control to do this.
I have derived from
BaseValidator which provides the base implementation for
all validation controls. It is not an abstract class. I have overridden only
two functions which are
first one checks the validity of the
ControlToValidate property and if you
return false in this function , the validation framework raises an exception.
is the main function which checks if
there is a selected item in the
ListControl. If the function doesn't
return false then this means
An interesting Point is since the
DropDownList control can
not have a
SelectedIndex property with a value of –1 , I checked against 0 so
you may want to insert an empty item in the beginning.
enough with the talk lets take a
look at the code.
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);
protected override bool EvaluateIsValid()
return (_listctrl is DropDownList) ? (_lisctrl.SelectedIndex != 0) :
( _lisctrl.SelectedIndex != -1);
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.