cancel
 

 

Job type :

Zip Code :

community forums

Featuring over 100 topics of interest to DoItYourselfers.

How to Organize the Controls into Validation Groups in ASP.NET

AlertThis content requires Flash

To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

Download the free Flash Player now!

Get Adobe Flash Player

Video Transcript

So, we have one more thing to think about in this example and then we are through. Sometimes in a webpage, you want to partition the page into separate panels. So for example, I might want to have this upper part of the webpage contain certain controls which when I click this submit button I want to validate but I might want to add another panel in the lower part of the webpage, containing a different set of data which I do not want to validate at the same time.

So, let me just illustrate that on the toolbox. I will go to the standard tab and I am going to drag a panel control, and I will just make it little bigger and I am going to add some controls to the panel. Let us add a textbox. Just for now, let us just the ID as textbox one and we will add a required field valid data on it. So in the validation top, required field valid data and let us just associate that with the textbox control that we just added. I would not bother setting the controls because we have done enough of that but you can see that this valid data is now associated with that textbox. And let us also, in the standard tab, add another button.

So in this example now, we have an upper section which contains information about the user and I want to validate that data when the user clicks that button. But I do not want to validate this data. I only want to validate this data when the user clicks that button. Okay, so this button should validate the upper controls and this button should validate the lower controls.

Unfortunately, that is not what happens by default. Let us run the application and see what happens, as it currently stands. So, I will just click the submit button here and you will see what happens. So I click that submit button and it validated all of these controls as we would expect. But also, it has validated this control and I really was not hoping that will happen, likewise, if I close the application and run it again. If I click this button, then it validates everything again. I was hoping that this button would just validate these controls but in fact, it has validated the entire page. And that is the default behavior that you will get.

In order to overcome this, you need to use validation groups. And now if you follow along quite carefully with me at this point, I am going to click on each control in the upper section and I am going to hold the control button down on the keyboard as I do it. So let us select each of the textboxes; the name and then the age, enrollment date, password, confirm, email address, security code, plus the validation controls required, range valid data, required range valid data, required comparison, required regular expression, custom valid data. Plus the button itself, plus the validation summary. All of those have been selected and carefully if I come down to the property window, I am going to set the validation group property for all of those controls to be the same validation group, and I will call it validation group one.

So, by doing that, when you click this button now, it will only validate controls in the same validation group and it will only use the validtors in the same validation group. Likewise, if I select the second panel, the textbox, control click, for the valid data, control click for the button. And I will put all of those controls into a different validation group called validation group two.

Such that when I click this button, it will only validate items in that validation group and it would not attempt to validate stuff in the first section. So let us run it again and cross our fingers and hope. I will click the first submit button and you can see it has validated the upper controls but it has not validated the lower control. And likewise, if I click the lower button, then it validates the lower section but it does not validate the upper section.

So this is just what we want and it enables us to have quite complex webpages with multiple panels, and each panel contains controls and valid data and buttons, and validation summaries in the same validation group so that they work independently of
So, we have one more thing to think about in this example and then we are through. Sometimes in a webpage, you want to partition the page into separate panels. So for example, I might want to have this upper part of the webpage contain certain controls which when I... click to read more


Your Winter Home Checklist

sponsored articles of the day

diy centers

Research and explore a wealth of wisdom on these topics