Jason Tucker's Blog

not at all creative...

I'm not sure I get this validation group stuff.

So we are trying to put the wraps on out ASP.Net 2.0 app for a client here and I thought it would be cool to check out the ValidationGroup thing that they added to the Validators. So lets say I have something like this for example:

<asp:DropDownList ID="survey_list" runat="server" />
<asp:RequiredFieldValidator InitialValue="" Id="RequiredFieldValidator3"
runat="Server" Display="dynamic"
ControlToValidate="survey_list" ErrorMessage="You must select a survey to copy to." />

<asp:DropDownList ID="survey_questionsstart" runat="server" OnDataBound="list_DataBound" />
<asp:RequiredFieldValidator InitialValue="" Id="RequiredFieldValidator1" runat="Server" ControlToValidate="survey_questionsstart" ValidationGroup="CopyByRange" ErrorMessage="You must select a start question." />

<asp:Button ID="btnAddByRange" runat="server" Text=" Copy " OnClick="btnAddByRange_Click" CausesValidation="true" ValidationGroup="CopyByRange" />

<asp:DropDownList ID="questionlist" runat="server" OnDataBound="list_DataBound" />
<br />
<asp:RequiredFieldValidator InitialValue="" Id="RequiredFieldValidator4" runat="Server" Display="dynamic" ControlToValidate="questionlist" ValidationGroup="CopySingleQuestion" ErrorMessage="You must select a question." />

So it's a dropdownlist of Surveys, followed by a question list and a button to copy a range of questions followed by another dropdown of questions and a button to copy a single question.

What I decided todo was have a ValidationGroup on each button so the validators could be seperated and give the user a rich UI so they can correct any mistakes they have.

Now what I would think would happen would be that if I click on either of the buttons the ValidationGroup would fire and then any items that don’t have a group would fire. Unfortunately that’s not what happens. It just looks at the group and then goes it merry way.

So if I want to have one control that needs to be validated on multiple conditions, like above and like so many other times in my career, I need to have two required validators for that item. Anyone know if what I want is possible. When my project winds down at the end of the week I hope to look into this some later.

Comments

Anderson Imes said:

I don't know if this is still the case in ASP.NET 2.0, but in 1.1 the CustomValidator would fire no matter what if you did NOT set the ControlToValidate property.

I've got kind of a poor description of the workaround here:
http://opinion.theimes.com/archive/2005/08/08.aspx

In terms of a solution, this is not really it.

When I run into a situation like this, I typically take it as a sign from the Usability Gods that I should probably try and simplify my user interface, but that's just me.
# November 7, 2005 5:03 PM

foobar said:

You need to add a validation summary control for each validation group.
# November 7, 2005 5:51 PM

sanshalim said:

You need to set the validationgroup property on the control as well for the Validation Group control to work.

# June 25, 2007 9:45 AM

durden said:

-foobar the ValidationSummary is optional and not needed in this case.

-sanshalim, no the code is correct you only need the ValidationGroup on the control that causes the postback and the validators that should be fired.

In this example the control that causes the postback for "CopySingleQuestion" validationgroup is missing...

Apart from that the functionality you ask for is not implemented (and in my career I never had the need to use it).

I would not use ValidationGroup in your case but I would do customvalidation only and check which button caused the postback in the code behind... that makes more sense to me.

# June 16, 2008 2:58 PM

weblogs.asp.net said:

ValidatorsSchmalidators.. I like it :)

# March 28, 2011 10:35 AM

weblogs.asp.net said:

ValidatorsSchmalidators.. Nifty :)

# April 25, 2011 5:56 PM

weblogs.asp.net said:

ValidatorsSchmalidators.. Huh, really? :)

# June 8, 2011 5:13 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)