简体   繁体   English

多个控件上的asp.net CustomValidator

[英]asp.net CustomValidator on multiple controls

I am working with a page that has a series of checkboxes that at least one box must be checked. 我正在使用具有一系列复选框的页面,必须至少选中一个复选框。

The validator fires when I click the submit button and if no checkboxes are checked indicates the validation has failed. 当我单击提交按钮时,验证器将触发,如果未选中任何复选框,则表明验证失败。 However If I check one of the checkboxes the validation message does not go away until I click the submit button again. 但是,如果我选中其中一个复选框,直到再次单击提交按钮,验证消息才会消失。

If I was using this on a control in which I had specified the ControlToValidate and I fixed the validation issue the error message goes away immediately. 如果我在已指定ControlToValidate的控件上使用了此控件,并且修复了验证问题,则错误消息会立即消失。

However in this case I am not setting the ControlToValidate due to the need to validate several independent controls. 但是在这种情况下,由于需要验证几个独立的控件,因此我没有设置ControlToValidate。

So my question is can I cause a re-validation of the custom validator? 所以我的问题是我可以引起自定义验证器的重新验证吗? For instance I would like to add on each checkbox a onclick="revalidate()" that would then force the validation to happen again. 例如,我想在每个复选框上添加一个onclick =“ revalidate()”,然后将强制再次进行验证。

Here is some sample code that I wrote to demonstrate the scenario. 这是我编写的一些示例代码来演示这种情况。

<script type="text/javascript">
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    function IsOneCheckboxChecked(sender, args) {
        if (!$('[id$=checkBox1]').attr('checked') &&
    !$('[id$=checkBox2]').attr('checked')) {
            args.IsValid = false;
        }
        else {
            args.IsValid = true;
        }
    }
</script>



<asp:CheckBox ID="checkBox1" runat="server" />
<br />
<asp:CheckBox ID="checkBox2" runat="server" />
<br />
<asp:CustomValidator ID="customValidatorMustBeOnCheckboxChecked" 
    runat="server" ClientValidationFunction="IsOneCheckboxChecked"
    ErrorMessage="You must select at least one checkbox option" 
    OnServerValidate="customValidatorMustBeOnCheckboxChecked_ServerValidate" />
<br />
<br />
<asp:Button ID="buttonSubmit" runat="server" Text="Submit" />

Yes there is a way of revalidating before the commit is clicked. 是的,在单击提交之前,有一种重新验证方法。 ASP.NET builds an 'onBlur' event that binds to the control you are validating in javascript, which runs the code you define in client validation function. ASP.NET构建一个'onBlur'事件,该事件绑定到您正在javascript中验证的控件,该事件运行您在客户端验证功能中定义的代码。 This is running but of course you are clicking a checkbox then submit again - onBlur only kicks in when you click elsewhere before clearing the message. 它正在运行,但是您当然要单击一个复选框,然后再次提交-onBlur仅在您清除消息之前单击其他位置时才会出现。 So I would call your client validation function on a mouseleave or mouseover event before the user gets to the submit button. 因此,在用户进入“提交”按钮之前,我将在mouseleave或mouseover事件上调用您的客户端验证功能。 This would clear the message and keep your args.IsValid set correctly, so by the time the user clicks submit the error message is gone and the form validates. 这将清除该消息并正确设置您的args.IsValid,因此,当用户单击提交时,错误消息就消失了,并且表单已通过验证。

for example: 例如:

$(".submitButtonDiv").mouseenter(function () {
    IsOneCheckboxChecked();
});

Well I was able to solve the issue by finding the span that the control validator creates and in my revalidate method hide or expose the error message. 好了,我能够通过找到控件验证器创建的跨度并在我的revalidate方法中隐藏或暴露错误消息来解决此问题。 However I would like to think something better exists? 但是我想认为存在更好的东西?

I created a blog entry on this at: 我在以下位置为此创建了一个博客条目:

http://coding.infoconex.com/post/ASPNET-CustomValidator-that-validates-multiple-controls-using-both-Server-Side-and-Client-Side-scripting.aspx http://coding.infoconex.com/post/ASPNET-CustomValidator-that-validates-multiple-controls-using-both-Server-Side-and-Client-Side-scripting.aspx

 function Revalidate() {
        if (!$('[id$=checkBox1]').attr('checked')
        && !$('[id$=checkBox2]').attr('checked')) {
            var control = $('[id$=customValidatorMustBeOnCheckboxChecked]');
           control.css('display', 'inline');
        }
        else {
            var control = $('[id$=customValidatorMustBeOnCheckboxChecked]');
            control.css('display', 'none');
        }
    }

<asp:CheckBox ID="checkBox1" runat="server" onclick="Revalidate()" />
<br />
<asp:CheckBox ID="checkBox2" runat="server" onclick="Revalidate()" />

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

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