RequiredFieldValidator and preventing CausesValidation

jp2code picture jp2code · Jul 27, 2012 · Viewed 7.4k times · Source

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?

Answer

Scott Selby picture Scott Selby · Jul 27, 2012

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" >