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

Using Single Validator for Multiple Controls

By , 4 Mar 2010
 
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:
 
<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)

About the Author

Brij
Software Developer (Senior)
India India
Member
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 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: 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

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionI am getting a problem when I have multiple textboxesmemberamiSett29 Apr '10 - 2:52 
AnswerRe: I am getting a problem when I have multiple textboxesmentorBrij29 Apr '10 - 9:48 
GeneralRe: I am getting a problem when I have multiple textboxesmemberamiSett30 Apr '10 - 11:20 

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

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