ARTICLE

ValidationScriptGenerator in VB.Net

Posted by kuralmani.elango Articles | Visual Basic 2010 March 16, 2005
This article gives you a solution for generating a client side validation script in a component model that you can reuse in an ASP.NET project.
 
Reader Level:

Summary

This article gives you a solution for generating a client side validation script in a component model that you can reuse in an ASP.NET project.

Introduction


Every web based application should have a client side validation to reduce the load in server. It is not a good programming model to write a code in each and every page. The code should be reusable for the entire application. For this I have came up with a solution that will help you to generate client side validation script with very less code.

Requirements

  • .NET Framework 1.1.

Step by step procedure

The below is the procedure to use this component.

  • Add the reference of the DLL(ValidationScriptGenerator) to your project that has the aspx pages.
  • Include the js file(Functions.js) in the aspx page
  • Create a List Dictionary object and add the ID of the control and message to be displayed
  • Pass your List Dictionary object to the static method GetScriptBlock that will return a string.
  • Pass the returned string into the static method RegisterScriptBlock with your button ID as another parameter.

Code Summary

The below is the code for adding your controls to listdictionary.

Private objLDControls As ListDictionary = New
ListDictionary()
objLDControls.Add("First Name cannot be blank",
Me
.txtFirstName.ID)
objLDControls.Add("Gender cannot be blank",
Me
.ddlGender.ID)
objLDControls.Add("Description cannot be blank",
Me
.txtDesc.ID)

Don't forget to include the namespace System.Collections.Specialized since the ListDictionary belong to this namespace. Reason for choosing ListDictionary is to maintain the order (FIFO) of the controls (no other collection will maintain the order).

Private strScript As String=ScriptGenerator.GetScriptBlock(Me,objLDControls)ScriptGenerator.RegisterScriptBlock(Me.btnSubmit,Me
, strScript).

Public Methods Used

This method is used generate the script block.

Public Shared Function GetScriptBlock(ByVal page As Page, ByVal htControls As ListDictionary, ByVal functionName As String, ByVal pageSpecificScript As Integer) As
String
Dim
scriptBlock As String=
Nothing
Dim
strAlertMessage As String=String
.Empty
formName=GetFormName(page)
If Not formName Is Nothing
Then
For
Each deEntry As DictionaryEntry In
htControls
If TypeOf page.FindControl(deEntry.Value.ToString()) Is TextBox
Then
Dim
regControl As TextBox=CType
(page.FindControl(deEntry.Value.ToString()), TextBox)
scriptBlock=scriptBlock & GenerateTextBoxScript
deEntry,regControl,pageSpecificScript)
Else If TypeOf page.FindControl(deEntry.Value.ToString()) Is DropDownList
Then
Dim
regControl As DropDownList=CType
(page.FindControl(deEntry.Value.ToString()), DropDownList)
scriptBlock= scriptBlock & GenerateDropDownScript(deEntry,regControl,pageSpecificScript)
Else If TypeOf page.FindControl(deEntry.Value.ToString()) Is HtmlInputText
Then
Dim
regControl As HtmlInputText=CType
(page.FindControl(deEntry.Value.ToString()), HtmlInputText)
scriptBlock=scriptBlock & GenerateTextBoxScript
deEntry,regControl,pageSpecificScript)
End
If
Next
deEntry
If Not functionName Is Nothing
Then
scriptBlock=scriptBlock & functionName & ";"
End
If
End
If
Return
scriptBlock
End
Function

RegisterScriptBlock

This method is used to register the script block to the control.

Public Shared Sub RegisterScriptBlock(ByVal btnHolder As Control, ByVal page As Page, ByVal scriptBlock As String
)
If TypeOf btnHolder Is ImageButton
Then
Dim
btnSource As ImageButton=CType
(btnHolder, ImageButton)
btnSource.Attributes("onclick")="javascript:" & scriptBlock
Dim myHttpResponse As
HttpResponse = HttpContext.Current.Response
Dim myHtmlTextWriter As HtmlTextWriter = New
HtmlTextWriter
myHttpResponse.Output)
btnSource.Attributes.AddAttributes(myHtmlTextWriter)
Else If TypeOf btnHolder Is Button
Then
Dim
btnSource As Button=CType
(btnHolder, Button)
btnSource.Attributes("onclick")="javascript:" & scriptBlock
Dim myHttpResponse As
HttpResponse = HttpContext.Current.Response
Dim myHtmlTextWriter As HtmlTextWriter = New
HtmlTextWriter
myHttpResponse.Output)
btnSource.Attributes.AddAttributes(myHtmlTextWriter)
End
If
End
Sub

Private Methods Used

GenerateTextBoxScript

This method is used to generate script for TextBox control.

Private Shared Function GenerateTextBoxScript(ByVal message As String, ByVal regControl As TextBox) As
String
'The trim function is in the Functions.js file it has to be included in the aspx file
Dim line As String=
Nothing
line="trim(" & formName & "." & regControl.ID & ".value)=='' || " & formName & "." & regControl.ID & ".value=='12/31/2025'"
Return
"if(" & line & "){ alert('" & message & "');" & formName & "." & regControl.ID & ".focus();return false}"
End
Function

GenerateDropDownScript

This method is used to generate script for DropDown control.

Private Shared Function GenerateDropDownScript(ByVal deEntry As DictionaryEntry, ByVal regControl As DropDownList, ByVal pageSpecificScript As Integer) As
String
Dim
line As String=
Nothing
Dim
result As String=
Nothing
line=formName & "." & regControl.ID & ".selectedIndex=='0'"
result= "if(" & line & "){ alert('" & deEntry.Key.ToString() & "');" & formName & "." & regControl.ID & ".focus();return false}"
Return
result
End
Function

Limitations

  • This component will validate only empty fields.
  • The first element of the Dropdown should have the value 0(i.e. <Select > value should be '0')

Conclusion

My intention of this article is not to reinvent the wheel. Use this component to cut down development time. Meanwhile I will enhance this component for more validation.

NOTE: THIS ARTICLE IS CONVERTED FROM C# TO VB.NET USING A CONVERSION TOOL. ORIGINAL ARTICLE CAN BE FOUND ON C# CORNER (WWW.C-SHARPCORNER.COM).

share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Team Foundation Server Hosting
Become a Sponsor