ListControl SelectedItem Validator
page 1 of 2
Published: 15 Dec 2003
Unedited - Community Contributed
Abstract
This modification of RequiredField Validator for ASP.NET ListControl adapts it to work for lists (e.g., DropdownList) without the user typing text in a Textbox.
by CENK CIVICI
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 12907/ 19
Article Contents:

Introduction

As you all know ASP.NET RequiredFieldValidator control checks that the validated control if it contains a value and for instance displays an error message if the user has not typed in text in a Textbox But the problem is RequiredFieldValidator does not work for ListControl derived controls (except ListBox) such as

* DropdownList
* CheckBoxList
* RadioButtonList

And actually it throws an exception in

* 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 checkes the validity of ControlToValidate property and if you return false in this function, the validation framework raises an exception. EvaluateIsValid returns the main function which checks if there is a selecteditem in the ListControl. If not returns false which means validation fails. And an interesting Point is since 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.


View Entire Article

User Comments

No comments posted yet.

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-28 11:54:07 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search