Click here to Skip to main content
15,896,606 members
Articles / Web Development / ASP.NET

ASP.NET Required TextBox (Graphical RequiredFieldValidator)

Rate me:
Please Sign up or sign in to vote.
4.96/5 (12 votes)
21 Sep 2006CPOL9 min read 140.3K   1.3K   60  
An ASP.NET custom TextBox control with a built-in RequiredFieldValidator, providing a similar look and feel to the Windows Forms ErrorProvider.
/*
==============================================================================
	Click2Install Software Custom ASP.NET Control Library
	
  Class:         RequiredTextBox.cs
  Functionality: Custom Designed ASP.NET TextBox with built-in RequiredField 
                 Validation.
  
  Copyright � 2001-2006 Click2Install Software
	Copyright � Maurice Moore (info@click2install.com)
		
	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.
	
	See the GNU General Public License for more details.
	
==============================================================================
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;
using click2install.design;
using System.Drawing.Design;

namespace click2install.controls
{
  [
   ToolboxData("<{0}:RequiredTextBox runat=server></{0}:RequiredTextBox>"),
   Designer(typeof(RequiredTextBoxDesigner)),
  ]
  public class RequiredTextBox : TextBox
  {

    #region constants

    private const string DEFAULT_VALIDATOR_TEXT = "&nbsp;*";

    #endregion

    public RequiredTextBox()
    {
      ValidatorDisplayType = ValidatorDisplay.Dynamic;
      ValidatorEnableClientScript = true;
      ValidatorFocusOnError = true;
    }


    #region Required Field Validator properties

    [
     Browsable(false),
    ]
    private RequiredValidator m_RequiredFieldValidator = null;
    /// <summary>
    /// Gets or sets the required text box validator.
    /// </summary>
    /// <value>The required text box validator.</value>
    private RequiredValidator RequiredTextBoxValidator
    {
      get
      {
        if (m_RequiredFieldValidator == null) { m_RequiredFieldValidator = new RequiredValidator(this); }
        return m_RequiredFieldValidator;
      }
      set { m_RequiredFieldValidator = value; }
    }

    /// <summary>
    /// Gets a value indicating whether this instance is design mode.
    /// </summary>
    /// <value>
    /// 	<c>true</c> if this instance is design mode; otherwise, <c>false</c>.
    /// </value>
    internal bool IsDesignMode
    {
      get { return DesignMode; }
    }

    private ErrorProviderType m_ErrorProvider = ErrorProviderType.StillIcon;
    /// <summary>
    /// Gets or sets the error provider.
    /// </summary>
    /// <value>The error provider.</value>
    [
     Description("The type of visual alert that will be shown when validation fails"),
     Category("Validator"),
     DefaultValue(ErrorProviderType.StillIcon),
    ]
    public ErrorProviderType ErrorProvider
    {
      get { return m_ErrorProvider; }
      set 
      {
        if (value == ErrorProviderType.Text) { RequiredTextBoxValidator.Text = DEFAULT_VALIDATOR_TEXT; }
        m_ErrorProvider = value; 
      }
    }

    /// <summary>
    /// Gets or sets the validator message.
    /// </summary>
    /// <value>The validator message.</value>
    [
     Description("The Validators Message when validation fails"),
     Category("Validator"),
    ]
    public string ValidatorMessage
    {
      get { return RequiredTextBoxValidator.ErrorMessage; }
      set { RequiredTextBoxValidator.ErrorMessage = value; }
    }

    /// <summary>
    /// Gets or sets the type of the validator display.
    /// </summary>
    /// <value>The type of the validator display.</value>
    [
     Description("The Validators Display"),
     Category("Validator"),
    ]
    public ValidatorDisplay ValidatorDisplayType
    {
      get { return RequiredTextBoxValidator.Display; }
      set { RequiredTextBoxValidator.Display = value; }
    }


    /// <summary>
    /// Gets or sets the validator text.
    /// </summary>
    /// <value>The validator text.</value>
    [
     Description("Whether the ErrorProvider icons are rendered in Design Mode or not."),
     Category("Validator"),
    ]
    public bool RenderDesignModeValidatorIcon
    {
      get { return RequiredTextBoxValidator.RenderDesignModeErrorProvider; }
      set { RequiredTextBoxValidator.RenderDesignModeErrorProvider = value; }
    }

    /// <summary>
    /// Gets or sets a value indicating whether [validator focus on error].
    /// </summary>
    /// <value>
    /// 	<c>true</c> if [validator focus on error]; otherwise, <c>false</c>.
    /// </value>
    [
     Description("The Validators FocusOnError"),
     Category("Validator"),
    ]
    public bool ValidatorFocusOnError
    {
      get { return RequiredTextBoxValidator.SetFocusOnError; }
      set { RequiredTextBoxValidator.SetFocusOnError = value; }
    }

    /// <summary>
    /// Gets or sets a value indicating whether [validator enable client script].
    /// </summary>
    /// <value>
    /// 	<c>true</c> if [validator enable client script]; otherwise, <c>false</c>.
    /// </value>
    [
     Description("The Validators EnableClientScript"),
     Category("Validator"),
    ]
    public bool ValidatorEnableClientScript
    {
      get { return RequiredTextBoxValidator.EnableClientScript; }
      set { RequiredTextBoxValidator.EnableClientScript = value; }
    }

    #endregion

    /// <summary>
    /// Raises the <see cref="E:System.Web.UI.Control.Init"></see> event.
    /// </summary>
    /// <param name="e">An <see cref="T:System.EventArgs"></see> object that contains the event data.</param>
    protected override void OnInit(EventArgs e)
    {
      Controls.Clear();

      this.ID = base.UniqueID;
      RequiredTextBoxValidator.TextBox = this;

      RequiredTextBoxValidator.ControlToValidate = this.ID;
      RequiredTextBoxValidator.EnableClientScript = ValidatorEnableClientScript;
      RequiredTextBoxValidator.Enabled = true;
      RequiredTextBoxValidator.ValidationGroup = this.ValidationGroup;
      RequiredTextBoxValidator.Text = (ErrorProvider == ErrorProviderType.Text) ? DEFAULT_VALIDATOR_TEXT : string.Empty;
      RequiredTextBoxValidator.ErrorMessage = ValidatorMessage;
      RequiredTextBoxValidator.Display = ValidatorDisplayType;
      RequiredTextBoxValidator.SetFocusOnError = ValidatorFocusOnError;

      Controls.Add(RequiredTextBoxValidator);
    }

    /// <summary>
    /// Renders the <see cref="T:System.Web.UI.WebControls.TextBox"></see> control to the specified <see cref="T:System.Web.UI.HtmlTextWriter"></see> object.
    /// </summary>
    /// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"></see> that receives the rendered output.</param>
    protected override void Render(HtmlTextWriter writer)
    {
      writer.RenderBeginTag(HtmlTextWriterTag.Span);

      base.Render(writer);

      RequiredTextBoxValidator.RenderControl(writer);

      writer.RenderEndTag();
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions