Tuesday, June 7, 2011

Advanced TextBox for Asp.Net ( TextBox with built in Validation )

Advanced TextBox Control for Asp.Net (Asp.Net Custom Server Control)
Hello friends, we all have been using Asp.Net validation controls with TextBox controls. By default, we have to configure several properties to do a validation using any validation controls. Here I'm introducing a new Custom Server Control which is derived from TextBox control. This control have some additional properties under the Advanced tab on the properties window.
The given below example shows how to create (or setup) an AdvancedTextBox control with builtin logic for validations. AdvancedTextBox control uses the TextBox and validation controls built with Asp.Net .
To develop this control, create a Asp.Net Server Control Project and write the following code.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AdvancedTextBox
{
public enum TextType
{
String,
Int,
Double,
Date,
Email,
Url
}

[DefaultProperty("Text")]
[ToolboxData("<{0}:AdvancedTextBox runat=server></{0}:AdvancedTextBox>")]
public class AdvancedTextBox : TextBox
{
private TextType _textType = TextType.String;
private bool _isRequired = false;
List<Control> _controls = new List<Control>();

[Category("Advanced")]
[Description("Text type for the TextBox")]
public TextType TextType
{
set
{
_textType = value;
}
get
{
return _textType;
}
}

[Category("Advanced")]
[Description("Mandatory field or not")]
public bool IsRequired
{
set { _isRequired = value; }
get { return _isRequired; }
}

[Category("Advanced")]
[Description("Minimum range value")]
public string MinimumValue
{
set;
get;
}

[Category("Advanced")]
[Description("Maximum range value")]
public string MaximumValue
{
set;
get;
}
[Category("Advanced")]
[Description("Error Message to be displayed when not entering value.")]
public string RequiredErrorMessage
{
set;
get;
}
[Category("Advanced")]
[Description("Error message to be displayed when type mismatch or range violation.")]
public string TypeOrRangeErrorMessage
{
set;
get;
}

[Category("Advanced")]
[Description("Sets the Text property of associated validation controls")]
public string ValidatorText
{
set;
get;
}

protected override void OnInit(EventArgs e)
{
GenerateControls();
base.OnInit(e);
}

protected void GenerateControls()
{
if (_isRequired)
{
RequiredFieldValidator req = new RequiredFieldValidator();
req.ControlToValidate = this.ID;
req.ForeColor = System.Drawing.Color.Red;
req.ValidationGroup = this.ValidationGroup;
req.ErrorMessage = this.RequiredErrorMessage;
req.Text = this.ValidatorText;
_controls.Add(req);
this.Controls.Add(req);
}
if (this.MinimumValue != null this.MaximumValue != null)
{
RangeValidator rag = new RangeValidator();
rag.ControlToValidate = this.ID;
rag.ForeColor = System.Drawing.Color.Red;
rag.ValidationGroup = this.ValidationGroup;
rag.ErrorMessage = this.TypeOrRangeErrorMessage;
rag.Text = this.ValidatorText;
switch (_textType)
{
case TextType.Int:
rag.Type = ValidationDataType.Integer;
break;
case TextType.Double:
rag.Type = ValidationDataType.Double;
break;
case TextType.Date:
rag.Type = ValidationDataType.Date;
break;
default: rag.Type = ValidationDataType.String;
break;
}

rag.MinimumValue = this.MinimumValue;
rag.MaximumValue = this.MaximumValue;
_controls.Add(rag);
this.Controls.Add(rag);
}
else
{
switch (_textType)
{
case TextType.String:

break;
case TextType.Int:
CompareValidator cv = new CompareValidator();
cv.ControlToValidate = this.ID;
cv.ValidationGroup = this.ValidationGroup;
cv.ForeColor = System.Drawing.Color.Red;
cv.ErrorMessage = this.TypeOrRangeErrorMessage;
cv.Type = ValidationDataType.Integer;
cv.Text = this.ValidatorText;
cv.Operator = ValidationCompareOperator.DataTypeCheck;
_controls.Add(cv);
this.Controls.Add(cv);
break;
case TextType.Double:
cv = new CompareValidator();
cv.ControlToValidate = this.ID;
cv.ValidationGroup = this.ValidationGroup;
cv.ForeColor = System.Drawing.Color.Red;
cv.ErrorMessage = this.TypeOrRangeErrorMessage;
cv.Type = ValidationDataType.Double;
cv.Text = this.ValidatorText;
cv.Operator = ValidationCompareOperator.DataTypeCheck;
_controls.Add(cv);
this.Controls.Add(cv);
break;
case TextType.Date:
cv = new CompareValidator();
cv.ControlToValidate = this.ID;
cv.ValidationGroup = this.ValidationGroup;
cv.ForeColor = System.Drawing.Color.Red;
cv.ErrorMessage = this.TypeOrRangeErrorMessage;
cv.Type = ValidationDataType.Date;
cv.Text = this.ValidatorText;
cv.Operator = ValidationCompareOperator.DataTypeCheck;
_controls.Add(cv);
this.Controls.Add(cv);
break;
case TextType.Email:
RegularExpressionValidator reg = new RegularExpressionValidator();
reg.ControlToValidate = this.ID;
reg.ForeColor = System.Drawing.Color.Red;
reg.ValidationGroup = this.ValidationGroup;
reg.ErrorMessage = this.TypeOrRangeErrorMessage;
reg.Text = this.ValidatorText;
reg.ValidationExpression = @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*";
_controls.Add(reg);
this.Controls.Add(reg);
break;
case TextType.Url:
reg = new RegularExpressionValidator();
reg.ControlToValidate = this.ID;
reg.ForeColor = System.Drawing.Color.Red;
reg.ValidationGroup = this.ValidationGroup;
reg.ErrorMessage = this.TypeOrRangeErrorMessage;
reg.Text = this.ValidatorText;
reg.ValidationExpression = @"http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&amp;=]*)?";
_controls.Add(reg);
this.Controls.Add(reg);
break;
default:
break;
};
}

}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
for (int i = 0; i < _controls.Count; i++)
{
_controls[i].RenderControl(writer);
}
}
}
}



When using this AdvancedTextBox control you can see the "Advanced" tab on the properties window.







  • IsRequired : when you set this property to true, then AdvancedTextBox come up with RequiredFieldValidator control.

  • MaximumValue and MinimumValue: when you set this property AdvancedTextBox come up with a RangeValidatior Control.

  • RequiredErrorMessage: Error message to be set on the Required field validation control.

  • TextType: When you set this property, Advanced TextBox come up with a CompareValidator control to ensure the data type.

  • TypeOrRangeErrorMessage: Error message to be set on either compare validator control or range validator control.

  • ValidatorText: This will be for setting the Text property of the validation controls that are come up with AdvancedTextBox control.

This control will help developers to develop GUI fastly and efficiently without specifying or declaring any validation controls to validate a TextBox.
Hope all of you are doing great programming with AdvancedTextBox Control. Feel free to ask questions and doubts. :-)

1 comment:

Washington said...

Simple the best.