Click here to Skip to main content
6,595,854 members and growing! (18,437 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, ASP.NET, Visual Studio, Dev
Posted:23 Mar 2002
Views:112,233
Bookmarked:29 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
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


Member

Location: United States United States

Other popular Validation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 19 of 19 (Total in Forum: 19) (Refresh)FirstPrevNext
GeneralHeard of Attributes? PinmemberRob van der Veer0:12 24 Sep '05  
GeneralThis is unnecessary for DropDownList WebControls PinmemberGerald Bryant7:48 19 May '05  
GeneralYet another solution PinmemberLordJM1:54 6 Apr '05  
GeneralRe: Yet another solution PinmemberMyster18:10 11 Apr '05  
Generalfixes required to make this work Pinmembermagister9:08 22 Feb '05  
GeneralIt is Server Side PinmemberGeorge Mamaladze23:48 2 Sep '04  
GeneralThis works PinmemberMichael Yourshaw18:24 25 Jun '03  
GeneralHas anybody got this to work PinmemberJefferys6:54 10 Mar '03  
GeneralRe: Has anybody got this to work PinsussAnonymous7:45 10 Mar '03  
GeneralRe: Has anybody got this to work PinmemberJefferys7:52 10 Mar '03  
GeneralRe: Has anybody got this to work PinmemberMyster20:29 12 Apr '05  
GeneralAnother solution... PinmemberCypher11:09 11 Feb '03  
GeneralRe: Another solution... PinmemberphishBone19:21 20 Aug '03  
GeneralRe: Another solution... PinsussAnonymous12:19 29 Jan '04  
GeneralRe: Another solution... Pinmembercosmoansaldi9:44 30 Jun '05  
General3.5 from 2 votes... PinmemberNish [BusterBoy]9:31 25 Mar '02  
GeneralRe: 3.5 from 2 votes... Pinmemberccivici18:13 25 Mar '02  
GeneralRe: 3.5 from 2 votes... PinmemberNish [BusterBoy]18:27 25 Mar '02  
GeneralRe: 3.5 from 2 votes... Pinmemberccivici18: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-2009
Web18 | Advertise on the Code Project