3

I've got a RequiredFieldValidator on a contact form.

It works as intended when people click 'Submit', but if they click 'Cancel' or any of the multiple menus on my form, the RequiredFieldValidator cancels the action.

I have already searched and found that I need to set the other controls on my form using CausesValidation = False (using this post), but do I have to do that for every control on my page?

What makes it worse is that the menus on my form are contained in a Master.Page, and they are mostly <DIV> style CSS buttons, but clicking any of the buttons causes the RequiredFieldValidator to fire and fail the form.

Shouldn't the default be False and I have to turn on which control sets the validation?

Community
  • 1
  • 1

2 Answers2

4

you can set validation groups

             <asp:TextBox ID="tb1" runat="server" ValidationGroup="ValidateMe" />
             <asp:TextBox ID="tb2" runat="server" />
             <asp:RequiredFieldValidator" ID="rfv1" runat="server" ControlToValidate="tb1" ValidationGroup="ValidateMe" />
               ...
             <asp:Button ID="btnSubmit" runat="server" ValidationGroup="ValidateMe" />

came from here

Edit , sorry I didn't put put this in code properly and it didn't display:

Or you can always use and handle those on client side if they are just cancel and stuff like that

         <input type="button" > 
Community
  • 1
  • 1
Scott Selby
  • 9,420
  • 12
  • 57
  • 96
  • The [ValidationGroup](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.validationgroup.aspx) worked. I don't understand how that eliminated the hyperlink behavior, though. –  Jul 27 '12 at 01:22
  • Because the hyperlinks do not "know" the validation group. They only validate the unnamed (empty string "") group by default. – intrepidis Apr 11 '13 at 09:14
  • Thanks - I had a similar issue w/ hyperlinks that navigate the tabs in the jqueryui tabs widget and ValidationGroup fixed the issue – ajwaka Apr 09 '14 at 14:24
1

You only need to set:

  CausesValidation = False

To your buttons performing an action

As an alternative, you could add a ValidationGroup attribute to your controls and buttons to control which buttons raise the validation. Only the controls matching the ValidationGroup specified will be validated

Jupaol
  • 21,107
  • 8
  • 68
  • 100
  • I only have 1 Cancel button. I never noticed this freakish behavior until I wasn't able to physically navigate off of the Contact form using any of my hyperlinks on the page. If I set `False` on the Cancel button, will that also apply to all of the links? –  Jul 27 '12 at 01:17
  • 1
    mm nope, either set `CausesValidation=false` to each button/link or set the `ValidaitonGroup` to your validators and controls. An alternative, you could iterate throgh all your controls on the form and set the `CausesValidation=false` then just override to `true` the buttons you would like to fire the validaiton – Jupaol Jul 27 '12 at 01:21
  • OK, another +1. That was what I was afraid I was going to have to do (iterate over all controls on the form and set `CausesValidation=False` - stupid ASP.NET). Luckily, I was able to use the [ValidationGroup](http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.basevalidator.validationgroup.aspx) concept presented by Scott Selby above, and it was able to solve my issue with less effort. Thanks! –  Jul 27 '12 at 01:25