ASP.NET Core 5.0 : MVC, Razor Pages, Web API, EF Core, Blazor, Design Patterns, and more. Private online coaching for software developers. Click here for more details.

<%@ Page %>

Writing Server Side Control Validator


ASP.Net provides certain server controls like RequiredFieldValidator that perform task of web form validation for us. We can also write our own server side validators according to our need to save coding. Following sections tell you how to create such a control that validates Date values.

Namespaces Involved

Following namespaces are involved for our example:
  • System.Web.UI
  • System.Web.UI.WebControls
  • Microsoft.VisualBasic

Source Code

Create following class in VS.NET or any text editor.
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports Microsoft.VisualBasic

Public Class CDateValidator
	Inherits System.Web.UI.WebControls.BaseValidator

	Private myTextBox As TextBox

	Protected Overrides Function 
	ControlPropertiesValid() As Boolean

		Dim ctrl As Control
		ctrl = FindControl(ControlToValidate)
		If Not ctrl Is Nothing Then
			myTextBox = ctrl
			Return True
			Return False
		End If
	End Function

	Protected Overrides Function 
	EvaluateIsValid() As Boolean

		Dim srcdate As String
		Dim destdate As Date
		srcdate = myTextBox.Text.ToString
		If IsDate(srcdate) Then
			destdate = CType(srcdate, Date)
			return True
			return False
		End If
	End Function
End Class

How it works?

In above code we have created a class called CDateValidator. This class inherits from System.Web.UI.WebControls.BaseValidator. This class contains two protected overridden functions.
  • ControlPropertiesvalid : This function calls the FindControl which looks for the TextBox control whose value is to be checked. This function returns a boolean value of True/False depending on the condition whether it locates the control in the page or not.
  • EvaluateIsValid : This function determines the value entered in the text box is a valid date or not. It uses VB function IsDate() for checking the value.

Using CDateValidator Control in web forms

Compile above class as library using command line or VS.NET. Copy the resultant DLL in the bin folder of your web application.

Before you use our control it needs to be registered in the web form using @Register directive.

<%@ Register 
  • TagPrefix : The name of the Tag by which we will refer this control in our asp page
  • NameSpace : The namespace which is used in the assembly (assumed to be MyNamespace in our example)
  • Assembly : The name of the assembly (MyAssembly.dll in our case)

Next, you have to create an instance of the control as shown below:

<asp:TextBox ID="TextBox1" Runat="server" />
ID="validator1" Runat="server" 
ErrorMessage="Invalid Date Entered!" />
Here, the usage is similar to any other validation control like RequiredField validator. To test the control run the web form and enter some invalid date. You should get our error message displayed when the form is posted back.


Here we saw how to create a validator control that validates our data at server side. Such controls need to inherit from BaseValidator class. This gives us way to reuse our code and ease web form validations.

Bipin Joshi is an independent software consultant, trainer, author, yoga mentor, and meditation teacher. He has been programming, meditating, and teaching for 24+ years. He conducts instructor-led online training courses in ASP.NET family of technologies for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get connected : Facebook  Twitter  LinkedIn  YouTube

Posted On : 17 May 2002

Tags : ASP.NET Web Forms Server Controls Custom Controls

Subscribe to our newsletter

Get monthly email updates about new articles, tutorials, code samples, and how-tos getting added to our knowledge base.


Receive Weekly Updates