6

I have a few section in an ASP.NET page and need to validate them separately.

Each section has it's own validation summary section so I thought of using the ValidationSummary tag with the ValidationGroup attribute but it does not work.

The following code works but validates all controls in the page:

<asp:TextBox ID="field1" runat="server" TabIndex="1" MaxLength="20" />
<asp:RequiredFieldValidator ID="field1RequiredValidator" ControlToValidate="field1" runat="server"
Display="None" ErrorMessage="mandatory 1" />

<asp:TextBox ID="field2" runat="server" TabIndex="2" MaxLength="20" />
<asp:RequiredFieldValidator ID="field2RequiredValidator" ControlToValidate="field2" runat="server"
Display="None" ErrorMessage="mandatory 2" />

....

<asp:ValidationSummary ID="validationSummary" HeaderText="Sumary" runat="server" />

While the following does not work (no validation whatsoever, on submit I just go to the next page in the wizard):

<asp:TextBox ID="field1" runat="server" TabIndex="1" MaxLength="20" />
<asp:RequiredFieldValidator ID="field1RequiredValidator" ControlToValidate="field1" runat="server"
Display="None" ErrorMessage="mandatory 1" ValidationGroup="xxxx" />

<asp:TextBox ID="field2" runat="server" TabIndex="2" MaxLength="20" />
<asp:RequiredFieldValidator ID="field2RequiredValidator" ControlToValidate="field2" runat="server"
Display="None" ErrorMessage="mandatory 2" ValidationGroup="xxxx" />

....

<asp:ValidationSummary ID="validationSummary" HeaderText="Sumary" runat="server" ValidationGroup="xxxx" />

What am I missing here? Is there extra setup needed or something else?

Brian Webster
  • 30,033
  • 48
  • 152
  • 225
user0971832
  • 101
  • 1
  • 1
  • 4

1 Answers1

16

The default behaviour in ASP.NET is that when the user clicks a button that has no ValidationGroup specified (and has CausesValidation set to true), all validation controls that do not belong to a validation group are validated.

Therefore, to validate a specific group, you need to set the ValidationGroup property of the button that should cause the validation (and also possibly the CausesValidation property).

See the MSDN documentation for Button.ValidationGroup for details and an example.

EDIT: If you need to validate ALL groups on the page, the easiest way is of course to not use validation groups at all. If you however want to validate only some (but more than one) groups, you can do it on the server (in the click handler of the button) by calling:

Validate("groupOne");
Validate("groupTwo");
// ...

Note that this won't trigger client-side validation. See for example this post for a discussion about triggering multiple validation groups on a single button click.

EDIT: I found a blog post describing how to build a reusable "multiple validation group button" for ASP.NET, complete with code. Haven't tried it, but it looks promising.

Community
  • 1
  • 1
Anders Fjeldstad
  • 10,724
  • 2
  • 33
  • 50
  • Hey... it works, didn't know I had to specify it on the button also. Thanks! But now I have another issue. I have more validation groups in my page. How do I trigger ALL of them with only one submit button? – user0971832 Jul 06 '11 at 11:26
  • 1
    if you have to trigger all validation from one button then why you specify different validation group for them . – rahularyansharma Jul 06 '11 at 11:32
  • 1
    @rahularyansharma: Because my page is divided in sections. Each section has its own ValidationSummary (i.e. the error summary must display in the same section as the fields that are in error) so I need to group them. But I also need all to be triggered by just one submit button. – user0971832 Jul 06 '11 at 11:35
  • I edited my answer with a link to a blog post that describes what I think you are trying to achieve. I suggest you check it out. – Anders Fjeldstad Jul 06 '11 at 11:46
  • @Anders Fjeldstad: I'll give it a try. Thanks! – user0971832 Jul 06 '11 at 11:53