Click here to Skip to main content
15,867,686 members
Articles / Web Development / ASP.NET
Tip/Trick

Using Single Validator for Multiple Controls

Rate me:
Please Sign up or sign in to vote.
4.80/5 (28 votes)
4 Mar 2010CPOL 57.1K   20   12
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...
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.

License

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


Written By
Software Developer (Senior)
India India
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

Comments and Discussions

 
Questioncan a single textbox be validated with multiple validations Pin
darshith13-Sep-12 0:53
darshith13-Sep-12 0:53 
AnswerRe: can a single textbox be validated with multiple validations Pin
Brij13-Sep-12 3:24
mentorBrij13-Sep-12 3:24 
GeneralReason for my vote of 4 Nice article, Great trick for rapid ... Pin
Pankaj_tiwari198827-Dec-11 23:26
Pankaj_tiwari198827-Dec-11 23:26 
Generaltb.Attributes.Add("onfocus", "HookUpControl(this,'" + Custom... Pin
Member 424563312-Feb-11 19:39
Member 424563312-Feb-11 19:39 
General Nice One..! Wat about created at design time..! Pin
sashidhar10-May-10 19:07
sashidhar10-May-10 19:07 
QuestionI am getting a problem when I have multiple textboxes Pin
amiSett29-Apr-10 2:52
amiSett29-Apr-10 2:52 
AnswerRe: I am getting a problem when I have multiple textboxes Pin
Brij29-Apr-10 9:48
mentorBrij29-Apr-10 9:48 
GeneralRe: I am getting a problem when I have multiple textboxes Pin
amiSett30-Apr-10 11:20
amiSett30-Apr-10 11:20 
GeneralValidatorHookupControl Pin
Giri Ganji8-Mar-10 22:09
Giri Ganji8-Mar-10 22:09 
GeneralRe: ValidatorHookupControl Pin
Brij8-Mar-10 22:35
mentorBrij8-Mar-10 22:35 
GeneralNice. But, I will Prefer VAB ( Validation Application Block) Pin
Abhijit Jana25-Feb-10 21:11
professionalAbhijit Jana25-Feb-10 21:11 
GeneralRe: Nice. But, I will Prefer VAB ( Validation Application Block) Pin
Brij25-Feb-10 21:43
mentorBrij25-Feb-10 21:43 
Abhijit Jana wrote:
But, I would like to use Enterprise Library Validation Application block if I need validation on my overall project.


Yes you can use.. Here my objective to write this,How we can achieve better performace with ASP.NET validators. Smile | :)

Anyways thanks for your support...
Cheers!!
Brij

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

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