This trick is very useful if we have multiple controls. Then we can use only one validator control to validate it. It will reduce the page size dramatically and improve the performance because every validator is rendered as span on the page and a page can contain hundreds of controls which makes the page very heavy.
So in my example, I have several textboxes(Dynamically created) and I am using only one custom validator for this.
My markup(aspx) page is:
<body>
<form id="form1" runat="server">
<div>
<asp:CustomValidator ID="CustomValidator1" runat="server" ></asp:CustomValidator>
<asp:ValidationSummary ID="ValidationSummary1" runat="server" />
</div>
</form>
</body>
I am associating the validator from server side on
onfocus
event of the Textboxes from server side as:
for (int i = 0; i < 10; i++)
{
TextBox tb = new TextBox();
tb.ID = "tb" + i.ToString();
tb.Attributes.Add("onfocus", "HookUpControl(this,'" + CustomValidator1.ClientID + "')");
Page.Form.Controls.Add(tb);
}
and I have Hookup
HookUpControl
like:
function HookUpControl(curObj,validatorClientID)
{
var validationControl = document.getElementById(validatorClientID);
validationControl.controltovalidate = curObj.id;
validationControl.clientvalidationfunction="validatetextbox";
validationControl.validateemptytext = "true";
ValidatorHookupControl(curObj, validationControl);
}
and I have client validation function as:
function validatetextbox(sender, args)
{
if(args.Value=="")
{
sender.errormessage="<b>Required Field Missing</b><br />This is required." ;
sender.innerHTML="<b>Required Field Missing</b><br />This is required." ;
args.IsValid = false;
return;
}
if(isNaN(args.Value))
{
sender.errormessage="<b>Invalid field</b><br />Only Numbers are alowed." ;
sender.innerHTML="<b>Invalid field</b><br />Only Numbers are alowed." ;
args.IsValid = false;
return;
}
if(Number(args.Value)< 1000)
{
sender.errormessage="<b>value can not be less than 1000</b><br />This is required." ;
sender.innerHTML="<b>value can not be less than 1000</b><br />This is required." ;
args.IsValid = false;
return;
}
}
Here in this trick, the main role is played by
ValidatorHookupControl(curObj, validationControl)
which is responsible for attaching the validator to the control.
I hope this will help a lot to improve the performance of the page for all.
Brij is a 3-times Microsoft MVP in ASP.NET/IIS Category and a passionate .NET developer. More than 6 years of experience in IT field, currently serving a MNC as a Tech Lead/Architect.
He is a very passionate .NET developer and have expertise over Web technologies like ASP.NET 2.0/3.5/4.0, jQuery, JSON, Javascript, IIS and related technologies. He is also a Exchange Server (EWS) Specialist. He has great experience in design patterns and N-Tier Architecture.
He is also certified as Microsoft Certified Technologies Specialist-ASP.NET and Microsoft Certified Technologies Specialist-WCF in .NET 4.0. He has also received several awards at various forums and his various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website www.asp.net.
He has done MCA from NIT Durgapur and completed his graduation from Lucknow University.
Learning new technologies and sharing knowledge excites him most. Blogging, solving problems at various forums, helping people, keeps him busy entire day.
Visit his Blog:
Code Wala
Area of Expertise :
C#, ASP.NET 2.0,3.5,4.0, AJAX, JQuery, JSON, XML, XSLT, ADO.Net, WCF, Active Directory, Exchange Server 2007 (EWS), Java script, Web Services ,Win services, DotnetNuke, WSS 3.0,Sharepoint Designer, SQL Server 2000/2005/2008