5,317,180 members and growing! (21,854 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

Enhancing ASP.NET Validators

By xicoloko

An article on extending the ASP.NET validators
C#.NET 1.0, Win2K, Windows, .NET, ASP.NET, Visual Studio, Dev

Posted: 5 Jul 2001
Updated: 5 Jul 2001
Views: 56,911
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 2.52 Rating: 3.60 out of 5
1 vote, 33.3%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
1 vote, 33.3%
4
1 vote, 33.3%
5

Sample Image - enhacedvalidator.gif

Introduction

ASP.NET has impressive improvements in Web development. Data validation, deployment, user controls and lots more. And best of all: you can extend the basic controls using the class hierarchy. In this article I'll show how to change the validator controls to give better visual feedback to the user. It will change the label color of an invalid field.

How?

To have the desired result I extended the validators to include 5 properties

LabelControl Identify the label control of the control to be validated
LabelColor The standard label color
ErrorLabelColor The color of the label when the control input is invalid
UseErrorIndicator Set if an error indicator should be used
ErrorIndicator Sets the error indicator. The error indicator is a prefix string that will be included in the label, ie '*'

To change the color of the label I wrote my EvaluateIsValid method, calling the base class implementation, changing the label according to the return value:

protected override bool EvaluateIsValid()
{
    bool result = base.EvaluateIsValid();

    Control control = FindControl(LabelControl);
    if (control != null)
    {
        Label label = control as Label;

        if (label != null)
        {
            string sColorName = result ? LabelColor : ErrorLabelColor;
            label.ForeColor = Color.FromName(sColorName);

            if (UseErrorIndicator)
            {
                string sLabelText = label.Text;

                if (result)
                {
                    if (sLabelText.StartsWith(ErrorIndicator))
                    {
                        sLabelText = sLabelText.Substring(ErrorIndicator.Length);
                    }
                }
                else
                {
                    if (!sLabelText.StartsWith(ErrorIndicator))
                    {
                        sLabelText = ErrorIndicator + sLabelText;
                    }
                }

                label.Text = sLabelText;
            }
        }
    }

    return result;
}

I've compiled the class into a library:

csc /out:bin\XWebControls.dll /t:library XWebControls.cs

To use it in a page you have to register the prefix:

<%@ Page Language="C#" %>
<%@ Register TagPrefix="XC" Namespace="XWebControls" 
                Assembly="XWebControls" %>
...
<XC:RequiredFieldValidator    runat="server"
    ErrorMessage="The Required Field is required"
    ControlToValidate="field1"
    LabelControl="labelField1"
    EnableClientScript="false"
    UseErrorIndicator="true"
    Display="none" />

The source file included just implements the RequiredFieldValidator and the CustomValidator, but it's easy to write the others validators, or even you own validator. The source was developed using the .NET Beta 2

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

xicoloko


XicoLoko is a brazilian developer based in Switzerland.

Occupation: Architect
Company: VisionOne AG
Location: Switzerland Switzerland

Other popular ASP.NET 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 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralWhen Enhanced asp.net Validators failedmemberLeela Krishna.Ch18:20 14 Dec '05  
GeneralClientSide ScriptingmemberKirk Quinbar5:04 27 Feb '03  

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

PermaLink | Privacy | Terms of Use
Last Updated: 5 Jul 2001
Editor: Chris Maunder
Copyright 2001 by xicoloko
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project