1. July 2010 22:49
This is more or less an "ASP.NET Tip" post for the RequiredFieldValidator. I was working on my knowledge base application and had the following setup:
- 1 ASPX page with a DataList, Panel and ModalPopupExtender
- 1 User control within the Panel (ASCX)
- 1 ModalPopupExtender on the page that had a PopupControlID pointed to the Panel
In my ASCX control I had a simple entry form with two textboxes and a submit and cancel button
The logic goes like this for the page:
- Load the DataList when the page is first visited
- when someone clicks on an item in the datalist fireoff the ItemCommand event for it
- Show the ModalPopup in the ItemCommandEvent
Nothing too crazy right? Well I added RequiredFieldValidators to my ASCX control for the two textboxes. Once I added those on the page every time I clicked the items in the datalist nothing would happen.
I forgot that when clicking on the items in the datalist that caused a postback would trigger the requiredfieldvalidator's on the page. Hence why nothing was happening, because the values were blank.
So how to solve this?
Simple, add a ValidationGroup property to the sections on the page you want to validate together. In my case I wanted a click of one button to validate the two textboxes. I didn't care if anything else was clicked on the page...I didn't need it to validate at that point.
It would look something like this:
Notice how all three controls have the same value for ValidationGroup. This will contain the validation for these controls to each other. Very useful when you have multiple ASCX controls or even multiple sections on pages that required different validation.