Getting Started:
We have created a separate class library project called MyControlsLibrary which will host all the server controls. We will call our control RequiredTextBox control. The RequiredTextBox control will derive from the ASP.NET TextBox control and add the validators as composite controls. Let's add a RequiredFieldValidator to the RequiredTextBox control.
Adding a RequiredFieldValidator to the RequiredTextBox Control:
Our first task is to add a RequiredFieldValidator to our new RequiredTextBox control. Here is the code:
public class RequiredTextBox : TextBox
{
public string RequiredTextBoxErrorMessage { get; set; }
private RequiredFieldValidator _requiredFieldValidator;
protected override void OnInit(EventArgs e)
{
EnsureChildControls();
base.OnInit(e);
}
protected override void OnPreRender(EventArgs e)
{
CreateRequiredFieldValidator();
CreateRegularExpressionValidator();
base.OnPreRender(e);
}
private void CreateRequiredFieldValidator()
{
_requiredFieldValidator = new RequiredFieldValidator
{
ControlToValidate = ID,
ErrorMessage = RequiredTextBoxErrorMessage
};
Controls.Add(_requiredFieldValidator);
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
_requiredFieldValidator.RenderControl(writer);
}
}
The first thing to notice is the name of the property "RequiredTextBoxErrorMessage". We have provided a descriptive property name instead of the simple "ErrorMessage". The reason is that we will be adding other validators which will also have the ErrorMessage property. So, we are differentiating the validator error message properties by using descriptive names.
The CreateRequiredFieldValidator method is fired on the PreRender event and is used to create the RequiredFieldValidator control and then add it to the controls collection.
Adding the RegularExpressionValidator Feature to the RequiredTextBox Control:
Just like we added the RequiredTextBoxValidator control to our RequiredTextBox control we can add a RegularExpressionValidator control. The RegularExpressionValidator will ensure that the TextBox input is valid against the validation expression set by the user.
Here is the complete code used by the RequiredTextBox control:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace MyControlsLibrary
{
public class RequiredTextBox : TextBox
{
public string RequiredTextBoxErrorMessage { get; set; }
public string RegularExpressionValidatorErrorMessage { get; set; }
public string ValidationExpression { get; set; }
private RequiredFieldValidator _requiredFieldValidator;
private RegularExpressionValidator _regularExpressionValidator;
protected override void OnInit(EventArgs e)
{
EnsureChildControls();
base.OnInit(e);
}
protected override void OnPreRender(EventArgs e)
{
CreateRequiredFieldValidator();
CreateRegularExpressionValidator();
base.OnPreRender(e);
}
private void CreateRegularExpressionValidator()
{
_regularExpressionValidator = new RegularExpressionValidator
{
ControlToValidate = ID,
ErrorMessage = RegularExpressionValidatorErrorMessage,
ValidationExpression = this.ValidationExpression
};
Controls.Add(_regularExpressionValidator);
}
private void CreateRequiredFieldValidator()
{
_requiredFieldValidator = new RequiredFieldValidator
{
ControlToValidate = ID,
ErrorMessage = RequiredTextBoxErrorMessage
};
Controls.Add(_requiredFieldValidator);
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
_requiredFieldValidator.RenderControl(writer);
_regularExpressionValidator.RenderControl(writer);
}
}
}
Using the RequiredTextBox Control:
Let's use our new RequiredTextBox control on a ASP.NET page. The first thing that you must do is add a reference to the MyControlsLibrary class library project. Now, you need to register the assembly to the page so you can use the new RequiredTextBox control.
<%@ Register Assembly="MyControlsLibrary" Namespace="MyControlsLibrary" TagPrefix="mycontrols" %>
And here is the usage of the control:
<form id="form1" runat="server">
<div>
<mycontrols:RequiredTextBox ID="txtName" runat="server" RegularExpressionValidatorErrorMessage="Please input only 4 numbers" ValidationExpression="/d{4}" RequiredTextBoxErrorMessage="This field cannot be left empty!" />
</div>
</form>
Conclusion:
In this article we build a custom RequiredTextBox server control which performs the RequiredFieldValidation and RegularExpressionValidation by setting few properties on the control. In the future articles we will demonstrate how to inject custom client side code with our web server control.
[Download Sample]
UPDATE:
It is a much better idea to create the child controls inside the CreateChildControls method than inside the PreRender event. Simply, overwrite the CreateChildControls method and create child controls.
protected override void CreateChildControls()
{
CreateRequiredFieldValidator();
CreateRegularExpressionValidator();
base.CreateChildControls();
}