|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
SummaryThis 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. IntroductionEvery 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 procedureThe below is the procedure to use this component:
Code SummaryThe below is the code for adding your controls to 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 Public Methods UsedGetScriptBlockThis 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;
}
RegisterScriptBlockThis 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 UsedGenerateTextBoxScriptThis method is used to generate script for 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}";
}
GenerateDropDownScriptThis method is used to generate script for 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
ConclusionMy 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.
|
||||||||||||||||||||||