ARTICLE
ValidationScriptGenerator in VB.Net
Tags: .NET, alert, DLL, Dropdown, FIFO, FindControl, List Dictionary, System.Collections, TextBox Control, ToString, trim, Validation Script, ValidationScriptGenerator, ValidationScriptGenerator in VB.Net., VB.NET
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.
Download
Files:
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
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).