![]() |
Web Development »
Validation »
General
Intermediate
ValidationScriptGeneratorBy E KuralManiThis 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. |
C#, Windows, .NET, ASP.NET, Visual Studio, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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.
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.
The below is the procedure to use this component:
ListDictionary object and add the ID of the control and message to be displayed.
ListDictionary object to the static method GetScriptBlock that will return a string.
RegisterScriptBlock with your button ID as another parameter.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).
This method is used to 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;
}
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);
}
}
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}";
}
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;
}
Dropdown should have the value 0 (i.e. <Select> value should be �0�).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.
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 15 Mar 2005 Editor: Sumalatha K.R. |
Copyright 2005 by E KuralMani Everything else Copyright © CodeProject, 1999-2009 Web21 | Advertise on the Code Project |