Click here to Skip to main content
Licence 
First Posted 5 Jul 2001
Views 68,294
Bookmarked 40 times

Enhancing ASP.NET Validators

By | 5 Jul 2001 | Article
An article on extending the ASP.NET validators

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

Architect
VisionOne AG
Switzerland Switzerland

Member

XicoLoko is a brazilian developer based in Switzerland.


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralWhen Enhanced asp.net Validators failed PinmemberLeela Krishna.Ch17:20 14 Dec '05  
hi XicoLoKo, I have seen your implementation of Enhancing asp.net validators. The idea seems really cool. But when I tried using it in one of my realtime appications I had faced a problem. Suppose a case where two extended validator controls(say one RequiredFieldValidator and one RegularExpressionValidator) have the same Label as their LabelControl. During the page evaluation one fails and the other succeeds. The one which fails sets its color to red and the one which succeeds sets its color back to black which is logically not correct. Do you thing there is a way out for this problem. If so please suggest me.
GeneralClientSide Scripting PinmemberKirk Quinbar4:04 27 Feb '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 6 Jul 2001
Article Copyright 2001 by xicoloko
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid