简体   繁体   English

如何绕过 ASP.NET 中按钮的验证?

[英]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

ASPX Page: ASPX 页面:

<asp:Button ID="buttonNew" runat="server" Text="New" CausesValidation="False" />

OR或者

CodeBehind Page: (.cs)代码隐藏页面:(.cs)

buttonNew.CausesValidation = false;

Check here to know more about Validated and Validating events for the controls.查看此处以了解有关控件的ValidatedValidating事件的更多信息。

Set the button.causesValidation to false.将 button.causesValidation 设置为 false。

this link 这个链接

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.

For More Info 更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何绕过Asp.net控件验证仅对单击按钮时的几个控件有效? - How to bypass Asp.net control validation only for few control on button click? ASP.Net表单验证-仅在单击按钮时绕过客户端 - ASP.Net form validation - bypass client side only on button click 在asp.net向导中单击下一步时,如何绕过页面上的验证? - How can i bypass validation on page when clicking next in a asp.net Wizard? 如何在asp.net按钮中使用Jquery Validation插件 - How to use Jquery Validation plugin in asp.net button 如何对asp.net中的特定按钮使用验证摘要? - How to use validation summary for particular button in asp.net? 如何使用ASP.NET和Ajax绕过CORS策略? - How can i bypass CORS Policy with ASP.NET and Ajax? 在 ASP.NET Core 中不需要时如何绕过身份验证中间件 - How to bypass authentication middleware when not needed in ASP.NET Core 动态绕过服务器端模型验证ASP.NET MVC - Bypass server-side model validation dynamically ASP.NET MVC 登录按钮在asp.net中调用验证控制,如何避免单击“注销”按钮时调用验证? - Logout button calls validation conrols in asp.net, how to avoid calling validations on click of Log out button? 在ASP.NET MVC中绕过必填属性 - Bypass required attribute in ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM