[英]How to bypass validation for a button in ASP.NET?
I have an ASP.NET form that takes input from a user.我有一个 ASP.NET 表单,它接受用户的输入。 There's a
Submit
button on the form and a button called Calc
which does a calculation to populate a text field.表单上有一个
Submit
按钮和一个名为Calc
的按钮,它执行计算以填充文本字段。 The problem I'm having is that on the form I have a set of <ASP:REQUIREDFIELDVALIDATOR>
validators and when the Calc
button is pressed the form gets validated.我遇到的问题是,在表单上我有一组
<ASP:REQUIREDFIELDVALIDATOR>
验证器,当按下Calc
按钮时,表单会得到验证。 I don't want the required fields to be validated when the Calc
button is pressed, only the Submit
button.我不希望在按下
Calc
按钮时验证必填字段,只需要Submit
按钮。 Any way around this?有什么办法解决这个问题吗?
将CausesValidation
属性设置为 false。
<asp:Button runat="Server" ... CausesValidation="False" />
Button.CausesValidation (如果我没记错的话)。
Try putting CausesValidation="false"
as a button attribute.尝试将
CausesValidation="false"
作为按钮属性。
Some sample code:一些示例代码:
http://weblogs.asp.net/scottgu/archive/2005/08/04/421647.aspx http://weblogs.asp.net/scottgu/archive/2005/08/04/421647.aspx
Set the button.causesValidation to false.将 button.causesValidation 设置为 false。
However, if all it is doing is calculating something based on user input then you shouldn't have it posting back at all.但是,如果它所做的只是根据用户输入计算某些内容,那么您根本不应该让它回传。 I would recommend using an HTML button and attach some javascript to it to do your work for you and then you won't have this problem.
我建议使用 HTML 按钮并在其上附加一些 javascript 来为您完成工作,然后您就不会遇到此问题。
While designing the button, you can set its property CausesValidation="false" to avoid validation on button click event.在设计按钮时,您可以设置其属性 CausesValidation="false" 以避免对按钮单击事件进行验证。 It does not allow to validation the server control and perform its click event only
它不允许验证服务器控件并仅执行其单击事件
你应该用这个
UseSubmitBehavior="False"
To disable validation in a specific control在特定控件中禁用验证
Set the control's CausesValidation property to false.将控件的 CausesValidation 属性设置为 false。
<asp:Button id="Button3" runat="server"
Text="Cancel" CausesValidation="False">
</asp:Button>
To disable a validation control禁用验证控件
Set the validation controls Enabled property to false.
To disable client-side validation禁用客户端验证
Set the validation controls EnableClientScript property to false.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.