Email Aaron Blake now at webmaster@aaronblake.co.uk
or contact me

AJAX Server Side Custom Validation ASP.net 2.0

Introduction

This tutorial will cover how to use the ServerSideValidationExtender to enable custom validator controls to work client side without post-back using AJAX.
By default, custom validator controls do not work client side, even if within an AJAX update panel.
To get these to work, we need to use a new control called the ServerSideValidationExtender.

Requirements

STEP 1 – ASP.net AJAX Extensions

Before you can add the AJAX Control Toolkit to your site, you need to set it up to work with ASP.net AJAX.
A guide to do this in .net 2.0 can be seen in the previous tutorial. 

STEP 2 – ASP.net AJAX Control Toolkit

Before you can add the AJAX ServerSideValidationExtender to your site, you need to set it up to work with AJAX Control Toolkit.
A guide to do this in .net 2.0 can be seen in the previous tutorial. 

STEP 3 – Add the dll’s

In your web-site’s bin folder, add the following files from the Validation Guidance Bundle download:
  • AjaxControlToolkit.WCSFExtensions.dll
  • Microsoft.Practices.EnterpriseLibrary.Common.dll
  • Microsoft.Practices.EnterpriseLibrary.Validation.dll
  • Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet.dll
  • Microsoft.Practices.ObjectBuilder.dll

STEP 4 – Add Controls to the Visual Studio Toolbox.

Add the controls to the Visual Studio Toolbox by doing the following:
  • Right click on the toolbox and select “Add Tab”
  • Rename the new tab whatever you want, something like AJAX Validation Controls.
  • Now right click within that tab and select “Choose items..”
  • This will bring up a window, click on the browse button in the .net Framework Components tab and select the AjaxControlToolkit.WCSFExtensions.dll.
  • This will add the controls to the toolbox, enabling you to drag and drop them into web pages.

STEP 5 – Register dll in page to be used

If you drag the ServerSideValidationExtender from the toolbox, this step should happen automatically, however if it doesn’t then make sure you do the following.
At the top of the page you wish to use the controls, add the register prefix:

<%@
Register
Assembly=”AjaxControlToolkit.WCSFExtensions”
Namespace=”AjaxControlToolkit.WCSFExtensions”
TagPrefix=”ajaxtoolkitwcsfextensions”
%>

STEP 6 – Add control to page

You will now be able to use the ServerSideValidationExtender that page within your site.
Just add one, for example:

AJAX Server Side Validation

This is an example of using server side validation, client side by using ASP.net AJAX and the validation extension controls.
Code Behind:

Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs)
	Handles CustomValidator1.ServerValidate
	args.IsValid = validateNumber()
End Sub

Function validateNumber() As Boolean
	If IsNumeric(TextBox1.Text) Then
		Return True
	Else
		Return False
	End If
End Function

Further Info

Post a Comment

You must be logged in to post a comment.

Email me through this magic form.