|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionASP.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
To change the color of the label I wrote my 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
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||