RequiredFieldValidator and preventing CausesValidation

若如初见. 提交于 2019-12-10 19:19:32

问题


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?


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/11680123/requiredfieldvalidator-and-preventing-causesvalidation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!