Click here to Skip to main content
13,052,870 members (67,120 online)
Click here to Skip to main content
Add your own
alternative version


31 bookmarked
Posted 23 Mar 2002

ListControl SelectedItem Validator

, 23 Mar 2002
Rate this:
Please Sign up or sign in to vote.
RequiredField Validator for ASP.NET ListControl derived webserver controls
<!-- Add the rest of your HTML here -->


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 for
derived controls (except ListBox) such as

  • DropdownList
  • CheckBoxList
  • RadioButtonList

And actually it throws an exception in the

name,String propertyName)
function if you try to set the ControlToValidate property of the RequiredFieldValidator control to one of the above controls. So I decided to write a server side validation control to do this.

Basically 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

  • ControlPropertiesValid()
  • EvaluateIsValid()

The first one checks the validity of the ControlToValidate property and if you return false in this function , the validation framework raises an exception. EvaluateIsValid 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 validation fails. 

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.

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
    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);     
              return false;  // raise exception
       protected override bool EvaluateIsValid()
            return (_listctrl is DropDownList) ? (_lisctrl.SelectedIndex != 0) : 
                                                 ( _lisctrl.SelectedIndex != -1);


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Cenk Civici
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHeard of Attributes? Pin
Rob van der Veer23-Sep-05 23:12
memberRob van der Veer23-Sep-05 23:12 
GeneralThis is unnecessary for DropDownList WebControls Pin
Gerald Bryant19-May-05 6:48
memberGerald Bryant19-May-05 6:48 
GeneralYet another solution Pin
LordJM6-Apr-05 0:54
memberLordJM6-Apr-05 0:54 
GeneralRe: Yet another solution Pin
Myster11-Apr-05 17:10
memberMyster11-Apr-05 17:10 
Generalfixes required to make this work Pin
magister22-Feb-05 8:08
membermagister22-Feb-05 8:08 
GeneralIt is Server Side Pin
George Mamaladze2-Sep-04 22:48
memberGeorge Mamaladze2-Sep-04 22:48 
GeneralThis works Pin
Michael Yourshaw25-Jun-03 17:24
memberMichael Yourshaw25-Jun-03 17:24 
GeneralHas anybody got this to work Pin
Jefferys10-Mar-03 5:54
memberJefferys10-Mar-03 5:54 
GeneralRe: Has anybody got this to work Pin
Anonymous10-Mar-03 6:45
sussAnonymous10-Mar-03 6:45 
GeneralRe: Has anybody got this to work Pin
Jefferys10-Mar-03 6:52
memberJefferys10-Mar-03 6:52 
GeneralRe: Has anybody got this to work Pin
Myster12-Apr-05 19:29
memberMyster12-Apr-05 19:29 
GeneralAnother solution... Pin
Cypher11-Feb-03 10:09
memberCypher11-Feb-03 10:09 
GeneralRe: Another solution... Pin
phishBone20-Aug-03 18:21
memberphishBone20-Aug-03 18:21 
GeneralRe: Another solution... Pin
Anonymous29-Jan-04 11:19
sussAnonymous29-Jan-04 11:19 
GeneralRe: Another solution... Pin
cosmoansaldi30-Jun-05 8:44
membercosmoansaldi30-Jun-05 8:44 
General3.5 from 2 votes... Pin
Nish [BusterBoy]25-Mar-02 8:31
memberNish [BusterBoy]25-Mar-02 8:31 
GeneralRe: 3.5 from 2 votes... Pin
ccivici25-Mar-02 17:13
memberccivici25-Mar-02 17:13 
GeneralRe: 3.5 from 2 votes... Pin
Nish [BusterBoy]25-Mar-02 17:27
memberNish [BusterBoy]25-Mar-02 17:27 
GeneralRe: 3.5 from 2 votes... Pin
ccivici25-Mar-02 17:23
memberccivici25-Mar-02 17:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170713.1 | Last Updated 24 Mar 2002
Article Copyright 2002 by Cenk Civici
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid