Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RequiredFieldValidator and preventing CausesValidation

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?


2 Answers

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" > 
like image 142
Scott Selby Avatar answered Nov 30 '25 17:11

Scott Selby


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

like image 26
Jupaol Avatar answered Nov 30 '25 17:11

Jupaol