简体   繁体   English

ASP.Net表单验证-仅在单击按钮时绕过客户端

[英]ASP.Net form validation - bypass client side only on button click

I have a form with a bunch of fields that I am using RequiredFieldValidator, RegularExpressionValidator, and CustomValidator. 我有一个带有一堆字段的表单,正在使用RequiredFieldValidator,RegularExpressionValidator和CustomValidator。

I want my form to perform client side checks when tabbing between fields (it currently does this), but I want to force a server side submit when the asp:button is clicked. 我希望表单在字段之间进行制表时执行客户端检查(它当前正在执行此操作),但是我想在单击asp:button时强制服务器端提交。 Right now, if a form field is determined invalid on the client side, the submit button doesn't do anything. 现在,如果确定表单字段在客户端无效,那么提交按钮将不执行任何操作。 I want it to submit the page and perform the server side check. 我希望它提交页面并执行服务器端检查。

The reason I want this to happen is because I want the page to go back to the top and display all possible issues and make it obvious to the user that there was a problem. 我希望发生这种情况的原因是,我希望页面返回顶部并显示所有可能的问题,并使用户清楚地知道存在问题。 Currently if they didn't see the client side error message, they may just click submit, see nothing happen, and end up confused. 当前,如果他们没有看到客户端错误消息,则可以单击“提交”,什么也看不见,最后感到困惑。

Example field on aspx Page: aspx页面上的示例字段:

<asp:TextBox MaxLength="30" ID="LNom" runat="server" /><asp:RequiredFieldValidator ID="reqLNom" runat="server" ErrorMessage="Last Name Required" ControlToValidate="LNom" /><asp:CustomValidator ID="valLNom" runat="server" ErrorMessage="Please enter a valid last name (less than 30 characters in length)." ControlToValidate="LNom" OnServerValidate="ValidationLastName" />
 <asp:Button ID="Submit" Text="Submit" runat="server" onclick="Submit_Click" />

Submit button Code Behind: 提交按钮背后的代码:

protected void Submit_Click(object sender, EventArgs e)
{
    Page.Validate();
    if (Page.IsValid)
    {
      // Do stuff
    }
}

Obviously there is a bit more to this, but you get the idea. 显然,这还有很多,但是您明白了。

Try asp.net ValidationSummary . 尝试asp.net ValidationSummary See http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.validationsummary(v=vs.110).aspx It does exactly how you want it. 请参阅http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.validationsummary(v=vs.110).aspx它确实满足您的要求。 It can be a pop up or inline notification that tells the user what s/he needs to fix. 它可以是弹出式通知或内联通知,告诉用户他/他需要修复的问题。

If you add 'CausesValidation="False"' to your asp:button declaration. 如果将“ CausesValidation =” False“”添加到asp:button声明中。 This will cause the postback to happen regardless of the outcome of client side validation 无论客户端验证的结果如何,都会导致发生回发

You can do this client side, no need for a post back. 您可以在客户端进行此操作,无需回发邮件。 Look into the ValidationSummary control: 查看ValidationSummary控件:

http://www.w3schools.com/aspnet/control_validationsummary.asp http://www.w3schools.com/aspnet/control_validationsummary.asp

声明:本站的技术帖子网页,遵循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 中按钮的验证? - How to bypass validation for a button in ASP.NET? 具有3个选项卡的ASP.Net MVC 3客户端验证 - ASP.Net MVC 3 client side validation with a 3 tabs form asp.net按钮单击客户端和服务器端代码 - asp.net button click client and server side code 即使提交失败,ASP.NET Core Unobtrusive客户端验证表单onsubmit也会触发 - ASP.NET Core Unobtrusive Client Side Validation Form onsubmit fires even on validation failure ASP.net MVC dateFormat验证问题,客户端验证 - ASP.net MVC dateFormat Validation issue, Client side Validation 如何在ASP.NET中的客户端提交表单之前进行验证 - How to do validation before form is submitted on client side in ASP.NET ASP.NET Core MVC-包含多个步骤的表单-如何在每个步骤中触发客户端验证 - ASP.NET Core MVC - Form With Multiple Steps - How to Fire Client Side Validation At Each Step 如何使用Asp.net在按钮单击上使用服务器端验证 - How to use server side validation on button click using Asp.net 是否可以在 asp.net 控件上进行仅客户端验证? - Is it possible to have client-side only validation on asp.net controls?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM