Click here to Skip to main content
Click here to Skip to main content

Using Single Validator for Multiple Controls

By , 4 Mar 2010
Rate this:
Please Sign up or sign in to vote.
Prize winner in Competition "Tips & Tricks Competition"
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:
    <form id="form1" runat="server">
        <asp:CustomValidator ID="CustomValidator1" runat="server" ></asp:CustomValidator>
        <asp:ValidationSummary ID="ValidationSummary1" runat="server" />
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 + "')");
and I have Hookup HookUpControl like:
function HookUpControl(curObj,validatorClientID)
        var validationControl = document.getElementById(validatorClientID);
        validationControl.controltovalidate =;
        validationControl.validateemptytext = "true";  
        ValidatorHookupControl(curObj, validationControl);
and I have client validation function as:
function validatetextbox(sender, args)
            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;
            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; 
        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; 
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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Software Developer (Senior)
India India
Brij is a Microsoft MVP in ASP.NET/IIS Category and a passionate .NET developer. Having around 5 years of experience in IT field, currently serving a MNC as a Sr. developer.
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
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: Brij's arena of .NET
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
Follow on   Twitter

Comments and Discussions

Questioncan a single textbox be validated with multiple validations Pinmemberdarshith13-Sep-12 0:53 
AnswerRe: can a single textbox be validated with multiple validations PinmentorBrij13-Sep-12 3:24 
GeneralReason for my vote of 4 Nice article, Great trick for rapid ... PinmemberPankaj_tiwari198827-Dec-11 23:26 
Generaltb.Attributes.Add("onfocus", "HookUpControl(this,'" + Custom... PinmemberMember 424563312-Feb-11 19:39 
General Nice One..! Wat about created at design time..! Pinmembersashidhar10-May-10 19:07 
QuestionI am getting a problem when I have multiple textboxes PinmemberamiSett29-Apr-10 2:52 
AnswerRe: I am getting a problem when I have multiple textboxes PinmentorBrij29-Apr-10 9:48 
GeneralRe: I am getting a problem when I have multiple textboxes PinmemberamiSett30-Apr-10 11:20 
GeneralValidatorHookupControl PinmemberGiri Ganji8-Mar-10 22:09 
GeneralRe: ValidatorHookupControl PinmentorBrij8-Mar-10 22:35 
GeneralNice. But, I will Prefer VAB ( Validation Application Block) PinmvpAbhijit Jana25-Feb-10 21:11 
GeneralRe: Nice. But, I will Prefer VAB ( Validation Application Block) PinmemberBrij25-Feb-10 21:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 4 Mar 2010
Article Copyright 2010 by Brij
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid