简体   繁体   English

如何绕过Asp.net控件验证仅对单击按钮时的几个控件有效?

[英]How to bypass Asp.net control validation only for few control on button click?

I have an ASP.NET form that takes input from a user. 我有一个ASP.NET表单,该表单需要用户输入。 There is a Save & Add button on the form to perform different functionalities. 表单上有一个“保存并添加”按钮以执行不同的功能。

The problem I'm having is that on the form I have a set of validators and when the Add button is pressed the form gets validated. 我遇到的问题是在表单上我有一组验证器,并且当按下“添加”按钮时,该表单得到了验证。 There are 5 controls on the page but during Add button click , I need to validate only two controls. 页面上有5个控件,但在“添加”按钮单击期间,我仅需要验证两个控件。 Only during Save button click, I should validate all controls. 仅在单击“保存”按钮期间,我才应验证所有控件。

How to ignore those 3 control validation during Add button click. 单击添加按钮时如何忽略这3个控件的验证。

Any way around this? 可以解决吗?

To skip some of the validations I suppose you might want to use ValidatorEnable method in jquery to enable/disable. 我想跳过一些验证,您可能想在jquery使用ValidatorEnable方法来启用/禁用。 Something like this : 这样的东西:

Email:
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="valEmail" ControlToValidate="txtEmail" runat="server" 
     ErrorMessage="*Required" ForeColor="Red" ValidationGroup="Group2" />
<br />
Enable Validation:
<input type="checkbox" id="CheckBox2 checked="checked" />
<br />
<asp:Button Text="Submit" runat="server" ValidationGroup="Group2" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
    $(document).on("click", "#CheckBox2", function () {
        var valEmail = $("[id*=valEmail]");
        ValidatorEnable(valEmail[0], $(this).is(":checked"));
    });
</script>

Here is the live Demo in case you want to test it before heading towards its implementation: https://www.aspsnippets.com/demos/642/ 这是现场演示,以备您在实施之前进行测试: https : //www.aspsnippets.com/demos/642/

EDIT 编辑

To achieve this task using pure javascript and not jquery you can do something like this: 要使用纯javascript而不是jquery来完成此任务,您可以执行以下操作:

ValidatorEnable(document.getElementById('<%=yourValidator.ClientID%>'), false);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM