There are probably numerous options; I can think of 3:
1) add a label (let’s call it lblErrorMarker) beside the TextBox on the
webform and upon postback perform a check similar to this:
Page.Validate();
if (!Page.IsValid )
{
if (!Page.Validators [0].IsValid ) {
lblErrorMarker.Text= "*";
lblErrorMarker.BackColor= System.Drawing.Color.Red;
}
}
2) modify your control to become composite control that renders not only a
textbox but also a label beside it. This option however would limit the
capacity of the users of your control to style their own label.
3) modify your control to add an event when there is an error and provide an
event delegate for the users of your control to implement a method that adds
an "*" to a label. (Though I think this would be an overkill). For a sample
on eventdelegates refer to this sample from my website:
http://www.societopia.net/Samples/DataGridEventDelegates.aspx --
HTH,
Phillip Williams
http://www.societopia.net http://www.webswapp.com [quoted text, click to view] "Shark Bait" wrote:
> I have a TextBox the implements IValidator and most everything works fine (it
> performs validation on the TextBox. Now I'd like to stick an "*" next to the
> TextBox when validation fails. Is it even possible? I'm starting to think I'm
> hosed.
>
> Here's the code...
> using System;
> using System.Drawing;
> using System.ComponentModel;
> using System.Web.UI.WebControls;
> using System.Web.UI;
> using System.Text.RegularExpressions;
>
> namespace MasterLibrary.WebCustomControls {
>
> public enum TextBoxType{FreeForm,NumbersOnly,Email}
>
> public class TextBox : System.Web.UI.WebControls.TextBox, IValidator{
>
> bool _isValid = true;
> string _errorMessage = "";
> bool _requiredField = false;
> string _friendlyFieldName = "";
> int _maxLength = 1;
> Color _validateHighlightColor = Color.White;
> TextBoxType _textBoxType;
>
> protected override void OnInit(EventArgs e) {
> base.OnInit(e);
> Page.Validators.Add(this);
>
> }
>
>
> protected override void OnUnload(EventArgs e) {
> if (Page != null) {
> Page.Validators.Remove(this);
> }
> base.OnUnload(e);
> }
>
>
> [Category("Company"), Description("A valid TextBoxType from the
> TextBoxType enum.")]
> public TextBoxType TextBoxType{
> get{return _textBoxType;}
> set{_textBoxType = value;}
> }
>
>
> [Category("Company"), Description("The number a characters that can be
> entered into this field. Must be more then 0 (zero).")]
> public override int MaxLength
> {
> get{return _maxLength;}
> set
> {
> if(value<=0)
> {
> _maxLength=1;
> base.MaxLength=_maxLength;
> throw(new Exception("You must specify a maximum length!"));
> }
> else
> {
> _maxLength = value;
> base.MaxLength=_maxLength;
> }
> }
> }
>
>
> [Browsable(false), Category("Company"), Description("Indicates if the
> control is valid. Can be used in validation on postback.")]
> public bool IsValid {
> get { return _isValid; }
> set {
> _isValid = value;
> if (!_isValid){
> //this.BackColor = _validateHighlightColor;
> this.BorderWidth=1;
> this.BorderColor= _validateHighlightColor;
> }else{
> this.BackColor = Color.White;
> }
> }
> }
>
>
> [Category("Company"), Description("The message that will be displayed for
> a required field.")]
> public string ErrorMessage{
> get { return _errorMessage; }
> set { _errorMessage = value; }
> }
>
>
> [Category("Company"), Description("Set to true if the field is required.")]
> public bool RequiredField {
> get { return _requiredField; }
> set { _requiredField = value; }
> }
>
>
> [Category("Company"), Description("This is the name used if presented to
> the user.")]
> public string FriendlyFieldName {
> get { return _friendlyFieldName; }
> set { _friendlyFieldName = value; }
> }
>
>
> [Category("Company"), Description("Set a color to set the background to if
> there is an error.")]
> public Color ValidateHighlightColor
> {
> get{return _validateHighlightColor;}
> set{_validateHighlightColor = value;}
> }
>
>
> public virtual void Validate(){
> this._isValid= true;
>
> if (this._requiredField) {
> bool isBlank = (this.Text.Trim() == "");
>
> if (isBlank){
> this.ErrorMessage = String.Format("{0}," + _errorMessage==""? " is a
> required field.":_errorMessage, this._friendlyFieldName);
> this._isValid = false;
> }
> }
>
> if(Regex.Match(this.Text.Trim(), @"[^\w\!@#$*()'%._\s]").Success){
> this.ErrorMessage = String.Format("{0}, contains illegal characters.",
> this._friendlyFieldName.Trim()== "" ? this.ID:this._friendlyFieldName);
> this._isValid = false;
> }
>
> if(_textBoxType == TextBoxType.FreeForm){
> // Nothing.
> }
> else if(_textBoxType == TextBoxType.Email){
> if(!Regex.IsMatch(this.Text.Trim(),
> @"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*")){
> this.ErrorMessage = String.Format("{0}, must contain a valid email
> address.", this._friendlyFieldName.Trim()== "" ?
> this.ID:this._friendlyFieldName);
> this._isValid = false;
> }
> }
> else if(_textBoxType == TextBoxType.NumbersOnly){
> if(!Regex.IsMatch(this.Text.Trim(), @"^\d+")){
> this.ErrorMessage = String.Format("{0}, must contain numbers only.",
> this._friendlyFieldName.Trim()== "" ? this.ID:this._friendlyFieldName);
> this._isValid = false;
> }
> }
>
> }
>
>
> }
> }
>
> Thanks for your thoughts.