Online courses in ASP.NET MVC, ASP.NET Core, and Design Patterns conducted by Bipin Joshi. Read more...
Learn ASP.NET MVC / Core, jQuery, Angular, and Design Patterns through our online training programs. Courses conducted by Bipin Joshi on weekends. Read more details here.

<%@ Page %>

Writing Server Side Control Validator

Introduction

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
		Else
			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
		Else
			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="MyPrefix" 
NameSpace="MyNamespace" 
Assembly="MyAssembly"%>
Here,
  • 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" />
<MyPrefix:CDateValidator 
ID="validator1" Runat="server" 
ControlToValidate="TextBox1" 
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.

Summary

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 a software consultant, trainer, author and a yogi having 21+ years of experience in software development. He conducts online courses in ASP.NET MVC / Core, jQuery, AngularJS, and Design Patterns. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced Yoga way of life he also teaches Ajapa Meditation to interested individuals. To know more about him click here.

Get connected : Twitter  Facebook  Google+  LinkedIn

Posted On : 17 May 2002



Tags : ASP.NET Web Forms Server Controls Custom Controls