Click here to Skip to main content
15,885,546 members
Articles / Web Development / ASP.NET
Article

ListControl SelectedItem Validator

Rate me:
Please Sign up or sign in to vote.
3.00/5 (20 votes)
23 Mar 20021 min read 144K   37   31   19
RequiredField Validator for ASP.NET ListControl derived webserver controls

Introduction

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

RequiredFieldValidator
does not work for
ListControl
derived controls (except ListBox) such as

  • DropdownList
  • CheckBoxList
  • RadioButtonList

And actually it throws an exception in the

CheckControlValidationProperty(String
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.

C#
//
// 
//
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);
       }
   }
}

License

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


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionHeard of Attributes? Pin
Rob van der Veer23-Sep-05 23:12
Rob van der Veer23-Sep-05 23:12 
GeneralThis is unnecessary for DropDownList WebControls Pin
Gerald Bryant19-May-05 6:48
Gerald Bryant19-May-05 6:48 
GeneralYet another solution Pin
LordJM6-Apr-05 0:54
LordJM6-Apr-05 0:54 
GeneralRe: Yet another solution Pin
Murray Roke11-Apr-05 17:10
Murray Roke11-Apr-05 17:10 
Generalfixes required to make this work Pin
magister22-Feb-05 8:08
magister22-Feb-05 8:08 
GeneralIt is Server Side Pin
George Mamaladze2-Sep-04 22:48
George Mamaladze2-Sep-04 22:48 
GeneralThis works Pin
Michael Yourshaw25-Jun-03 17:24
Michael Yourshaw25-Jun-03 17:24 
GeneralHas anybody got this to work Pin
Jefferys10-Mar-03 5:54
Jefferys10-Mar-03 5:54 
GeneralRe: Has anybody got this to work Pin
Anonymous10-Mar-03 6:45
Anonymous10-Mar-03 6:45 
GeneralRe: Has anybody got this to work Pin
Jefferys10-Mar-03 6:52
Jefferys10-Mar-03 6:52 
GeneralRe: Has anybody got this to work Pin
Murray Roke12-Apr-05 19:29
Murray Roke12-Apr-05 19:29 
GeneralAnother solution... Pin
Cypher11-Feb-03 10:09
Cypher11-Feb-03 10:09 
GeneralRe: Another solution... Pin
phishBone20-Aug-03 18:21
phishBone20-Aug-03 18:21 
GeneralRe: Another solution... Pin
Anonymous29-Jan-04 11:19
Anonymous29-Jan-04 11:19 
GeneralRe: Another solution... Pin
cosmoansaldi30-Jun-05 8:44
cosmoansaldi30-Jun-05 8:44 
General3.5 from 2 votes... Pin
Nish Nishant25-Mar-02 8:31
sitebuilderNish Nishant25-Mar-02 8:31 
GeneralRe: 3.5 from 2 votes... Pin
ccivici25-Mar-02 17:13
ccivici25-Mar-02 17:13 
GeneralRe: 3.5 from 2 votes... Pin
Nish Nishant25-Mar-02 17:27
sitebuilderNish Nishant25-Mar-02 17:27 
GeneralRe: 3.5 from 2 votes... Pin
ccivici25-Mar-02 17:23
ccivici25-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.