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

Code Summary:

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

ListDictionary objLDControls=
new ListDictionary() objLDControls.Add("First Name cannot be blank",this.txtFirstName.ID);
objLDControls.Add("Gender cannot be blank",
this.ddlGender.ID);
objLDControls.Add("Description cannot be blank",
this.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).

String strScript=ScriptGenerator.GetScriptBlock(this,objLDControls);
ScriptGenerator.RegisterScriptBlock(
this.btnSubmit,this, strScript);

Public Methods Used:

This method
is used generate the script block

public static string GetScriptBlock(Page page,ListDictionary htControls,string functionName,int pageSpecificScript)
{
string scriptBlock=null;
string strAlertMessage=string.Empty;
formName=GetFormName(page);
if(formName!=null)
{
foreach(DictionaryEntry deEntry in htControls)
{
if(page.FindControl(deEntry.Value.ToString()) is TextBox)
{
TextBox regControl=(TextBox)page.FindControl(deEntry.Value.ToString());
scriptBlock=scriptBlock+GenerateTextBoxScript(deEntry,regControl,pageSpecificScript);
}
else if(page.FindControl(deEntry.Value.ToString()) is DropDownList)
{
DropDownList regControl=(DropDownList)page.FindControl(deEntry.Value.ToString());
scriptBlock= scriptBlock + GenerateDropDownScript(deEntry,regControl,pageSpecificScript);
}
else if(page.FindControl(deEntry.Value.ToString()) is HtmlInputText)
{
HtmlInputText regControl=(HtmlInputText)page.FindControl(deEntry.Value.ToString());
scriptBlock=scriptBlock+GenerateTextBoxScript(deEntry,regControl,pageSpecificScript);
}
}
if(functionName!=null) scriptBlock=scriptBlock+functionName+";";
}
return scriptBlock;
}

RegisterScriptBlock

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

public static void RegisterScriptBlock(Control btnHolder,Page page,string scriptBlock)
{
if(btnHolder is ImageButton)
{
ImageButton btnSource=(ImageButton)btnHolder;
btnSource.Attributes["onclick"]="javascript:"+scriptBlock;
HttpResponse myHttpResponse = HttpContext.Current.Response;
HtmlTextWriter myHtmlTextWriter =
new HtmlTextWriter(myHttpResponse.Output);
btnSource.Attributes.AddAttributes(myHtmlTextWriter);
}
else if(btnHolder is Button)
{
Button btnSource=(Button)btnHolder;
btnSource.Attributes["onclick"]="javascript:"+scriptBlock;
HttpResponse myHttpResponse = HttpContext.Current.Response;
HtmlTextWriter myHtmlTextWriter =
new HtmlTextWriter(myHttpResponse.Output);
btnSource.Attributes.AddAttributes(myHtmlTextWriter);
}
}

Private Methods Used:

GenerateTextBoxScript

This method is used to generate script for TextBox control

private static string GenerateTextBoxScript(string message,TextBox regControl)
{
//The trim function is in the Functions.js file it has to be included in the aspx file
string line=null;
line="trim("+formName+"."+regControl.ID+@".value)=='' || "+formName+"."+regControl.ID+@".value=='12/31/2025'";
return "if("+line+"){ alert('"+message+"');"+formName+"."+regControl.ID+@".focus();return false}";
}

GenerateDropDownScript

This method is used to generate script for DropDown control

private static string GenerateDropDownScript(DictionaryEntry deEntry,DropDownList regControl,int pageSpecificScript)
{
string line=null;
string result=null;
line=formName+"."+regControl.ID+@".selectedIndex=='0'";
result= "if("+line+"){ alert('"+deEntry.Key.ToString()+"');"+formName+"."+regControl.ID+@".focus();return false}";
return result;
}

Limitations:

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.