5,427,813 members and growing! (16,796 online)
Email Password   helpLost your password?
Web Development » Validation » General     Intermediate

ListControl SelectedItem Validator

By Cenk Civici

RequiredField Validator for ASP.NET ListControl derived webserver controls
C#, Windows, .NET 1.0, .NET, Visual Studio, ASP.NET, Dev

Posted: 23 Mar 2002
Updated: 23 Mar 2002
Views: 102,239
Bookmarked: 27 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
25 votes for this Article.
Popularity: 4.32 Rating: 3.09 out of 5
4 votes, 20.0%
1
5 votes, 25.0%
2
2 votes, 10.0%
3
3 votes, 15.0%
4
6 votes, 30.0%
5

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.

//

// 

//

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

About the Author

Cenk Civici



Location: United States United States

Other popular Validation articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralHeard of Attributes?memberRob van der Veer0:12 24 Sep '05  
GeneralThis is unnecessary for DropDownList WebControlsmemberGerald Bryant7:48 19 May '05  
GeneralYet another solutionmemberLordJM1:54 6 Apr '05  
GeneralRe: Yet another solutionmemberMyster18:10 11 Apr '05  
Generalfixes required to make this workmembermagister9:08 22 Feb '05  
GeneralIt is Server SidememberGeorge Mamaladze23:48 2 Sep '04  
GeneralThis worksmemberMichael Yourshaw18:24 25 Jun '03  
GeneralHas anybody got this to workmemberJefferys6:54 10 Mar '03  
GeneralRe: Has anybody got this to worksussAnonymous7:45 10 Mar '03  
GeneralRe: Has anybody got this to workmemberJefferys7:52 10 Mar '03  
GeneralRe: Has anybody got this to workmemberMyster20:29 12 Apr '05  
GeneralAnother solution...memberCypher11:09 11 Feb '03  
GeneralRe: Another solution...memberphishBone19:21 20 Aug '03  
GeneralRe: Another solution...sussAnonymous12:19 29 Jan '04  
GeneralRe: Another solution...membercosmoansaldi9:44 30 Jun '05  
General3.5 from 2 votes...memberNish [BusterBoy]9:31 25 Mar '02  
GeneralRe: 3.5 from 2 votes...memberccivici18:13 25 Mar '02  
GeneralRe: 3.5 from 2 votes...memberNish [BusterBoy]18:27 25 Mar '02  
GeneralRe: 3.5 from 2 votes...memberccivici18:23 25 Mar '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 23 Mar 2002
Editor: Chris Maunder
Copyright 2002 by Cenk Civici
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project