Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Group validation in ASP.NET 2.0

This tutorial will teach you how to use the ValidationGroup property introduced in ASP.NET 2.0, which allows you to link validators to buttons, so that certain buttons in a form can only trigger certain validators.

On Wednesday, July 27th 2005 at 01:32 PM
By Andrew Pociu (View Profile)
*****   (Rated 4.5 with 30 votes)
Contextual Ads
More ASP.NET Resources
Advertisement
Download this project Download the code



Validation groups represent something new, introduced in ASP.NET 2.0. With this feature you can implement validation on groups of controls. Before ASP.NET 2.0, when submitting a form, all the controls on the page were being validated. So when you press the Submit button, each and every TextBox or other control that has a validator, was checked. However, this caused a problem when you had multiple forms on a page, with two or more submit buttons. Pressing one of the buttons would start to validate all the controls on a page, and that's not what we wanted.



So let's see how these validation groups work. Start a new Web Site in Visual Studio 2005:








Add two panels to the form and some TextBoxes in both. For each TextBox add a validator, I added RequiredFieldValidators because they are the most common ones. Also, add a button in each of the panel, so that our WebForm looks something like:







Be sure to set each validator to validate one of the TextBoxes. You can do this by setting the ControlToValidate property in the Properties window.

After each TextBox has its RequiredFieldValidator, compile and run the web application. Type something into the TextBoxes in Panel2 and leave the TextBoxes in Panel1 blank, because we are not interested in that form. Press the submit button (the second one, of course), and watch the result:







Bummer! Even though we pressed the second button, the validators in the first form reacted and the form didn't get submitted.

Here's
where the validation groups come in handy. Click the first button and in the Properties window scroll to the ValidationGroup property and give it a name, Form1:







Now do the same thing for the second button, set the ValidationGroup property to Form2.



We're not done yet. The RequiredFieldValidators also have a ValidationGroup property, we need to set this property of the RequiredFieldValidators from Panel1 to the same name we gave the submit button in Panel1: Form1. Same thing needs to be done for the validators in Panel2, set their ValidationGroup property to the same name as the second button: Form2. This way each button is linked to the validators it should fire when it is pressed.



You can compile and run the web application now.







As you can see in the screenshot above, clicking the first button will now fire only the validators that are grouped with that button.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by b on Tuesday, May 30th 2006 at 02:47 AM

b

by Malik on Thursday, August 17th 2006 at 07:40 AM

Can i use one RequiredFieldValidators for 2 text box's

by g on Sunday, September 10th 2006 at 01:32 PM

A Requiredfieldvalidator always validates 1 control (1 textbox in the example). You can not validate 2 textboxes with 1 validator. In our e-marketing web application myMailmarket(http://www.mymailmarket.be) we used a lot of validators, to restrict input of the users!

by santosh on Thursday, May 24th 2007 at 05:42 AM

Hi,

i am using lot of controls in registration form.all validations r check in one button.how can put group for only two fields..

ex.select industry..if i select otherindustry..than other text box is visible..at that time i need check other text box is empty or not

by nitin on Wednesday, December 5th 2007 at 02:12 AM

awesome artical .
helps a lot thanks.

by selvi on Wednesday, January 16th 2008 at 11:44 PM

hi this is useful to me. is anyone can clear about postback property to me well. a

by Moe on Thursday, January 17th 2008 at 10:26 AM

This was great, very well explained. Thanks!

by Jazz on Monday, May 12th 2008 at 03:45 PM

Can one control belong to two different validation groups?

by wewe on Saturday, May 24th 2008 at 04:08 AM

wew

by girl on Friday, August 29th 2008 at 01:01 PM

How can i use the same validations for more buttons, should i make more validation groups again, or the same one?

by yogini p. on Thursday, February 26th 2009 at 02:16 AM

Hi!
This was good. But I want to learn code-behind it. e.g if for password, I want to give validation i.e minimum character length should be 8 and if length is less than 8 character Error Message should get display in browser. Then What is the codebehind for such type of validation?

by yogini p. on Thursday, February 26th 2009 at 02:16 AM

Hi!
This was good. But I want to learn code-behind it. e.g if for password, I want to give validation i.e minimum character length should be 8 and if length is less than 8 character Error Message should get display in browser. Then What is the codebehind for such type of validation?

by furqan on Wednesday, April 29th 2009 at 01:52 PM

Salam,,Thanks for this information,,your site is helping me alot, thnks again..

by Daniel on Friday, June 19th 2009 at 05:28 AM

Hello. What about if you add a validationSummary on your page. Suppose you add a validationSummary for your group1. If you enter incorrect values in group1 you'll see the summary. Next, you add correct values to group2. Result: summary is disapear. How to avoid this? Thx

by phanf on Monday, August 24th 2009 at 07:46 PM

Great example, I was wondering if this could be done with using just 1 button on the page and having a radio button for the user to select the group before submitting.

by syed on Tuesday, October 6th 2009 at 06:29 AM

It was very nice and easy to follow.

by Mahesh on Thursday, November 19th 2009 at 02:21 AM

I have lot of textboxes and drop downs in my page.
I also have a grid view which has got one drop down and 5 textboxes which are editable. How can i give validations for these controls in the grid view? If any one of them is missing, then i should display an error message. Can anyone plz help me?

Thanks in advance
Mahesh

by Elli Benett on Monday, February 22nd 2010 at 08:58 AM

Hey guys. I have this tiny bit problem stuck here. See, I have finished my RSS feed, tested it and all works perfectly, but when I remove an article from my feed it still showed in the reader. How do i get the reader to see the article is no longer part of the feed. Any ideas what's wrong?
http://www.rsschannelwriter.com/

by Elli Benett on Monday, February 22nd 2010 at 08:59 AM

Hey guys. I have this tiny bit problem stuck here. See, I have finished my RSS feed, tested it and all works perfectly, but when I remove an article from my feed it still showed in the reader. How do i get the reader to see the article is no longer part of the feed. Any ideas what's wrong?
http://www.rsschannelwriter.com/

by Elli Benett on Monday, February 22nd 2010 at 08:59 AM

Hey guys. I have this tiny bit problem stuck here. See, I have finished my RSS feed, tested it and all works perfectly, but when I remove an article from my feed it still showed in the reader. How do i get the reader to see the article is no longer part of the feed. Any ideas what's wrong?
http://www.rsschannelwriter.com/

by Elli Benett on Monday, February 22nd 2010 at 08:59 AM

Hey guys. I have this tiny bit problem stuck here. See, I have finished my RSS feed, tested it and all works perfectly, but when I remove an article from my feed it still showed in the reader. How do i get the reader to see the article is no longer part of the feed. Any ideas what's wrong?
http://www.rsschannelwriter.com/

by Lalit on Tuesday, April 27th 2010 at 02:31 AM

How to use validation property in Grid view?


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs ASP.NET Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Sponsors
Discover Geekpedia

Other Resources