通过javascript重置asp.net验证控件? [英] Reset an asp.net validation control via javascript?

查看:64
本文介绍了通过javascript重置asp.net验证控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过JavaScript重置asp.net验证控件?当前代码示例清除了错误消息文本,但未重置下一个表单提交的验证控件。

How do I reset an asp.net validation control via JavaScript? The current code sample clears the error message text but does not reset the validation control for the next form submission.

var cv= document.getElementById("<%= MyValidationContorl.ClientID %>");
cv.innerHTML = '';

更新:

以下是表单的完整代码示例。我似乎无法在另一个表单提交中启动验证控件:

Here is the full code sample of the form. I can not seem to get the validation controls fire off on another form submission:

function ClearData() {
    var cv = document.getElementById("<%= MyValidationContorl.ClientID %>");
    cv.innerHTML = '';
}

<html>
   <form>
       <asp:TextBox id="MyTextControl" runat="server" />
       <asp:CustomValidator ID="MyValidationContorl" runat="server" />
       <input type="button" onclick="javascript:ClearCCData(); return false;" runat="server" />
   </form>
</html>


推荐答案

每次发帖都会触发页面验证,似乎问题是您正在清除验证器内容 cv.innerHTML =''; ,这样您的验证器消息将永远丢失,您认为验证是不再开火了。

Page validation is fired every time you do a post, what appears to be the problem is that you are clearing the validator content cv.innerHTML = '';, this way your validator message is lost forever and you'll think validation is not firing again.

并且对于@Glennular答案,如果代码设置为<$,则代码不处理验证器显示属性c $ c>动态验证器将使用 validator.style.display 切换,但如果设置为内联然后将使用 validator.style.visibility 属性。

and for @Glennular answer, the code does not handle the validator Display property, if its set to Dynamic the validator will be toggled using validator.style.display, but if its set to None or Inline then validator.style.visibility property will be used instead.

最好使用asp.net ValidatorUpdateDisplay 代替,

Its better to use asp.net ValidatorUpdateDisplay instead,

<script type="text/javascript">
    function Page_ClientValidateReset() {
        if (typeof (Page_Validators) != "undefined") {
            for (var i = 0; i < Page_Validators.length; i++) {
                var validator = Page_Validators[i]; 
                validator.isvalid = true;
                ValidatorUpdateDisplay(validator);
            }
        }
    }
</script>

更新:重置验证摘要

<script type="text/javascript">
function Page_ValidationSummariesReset(){
    if (typeof(Page_ValidationSummaries) == "undefined")
            return;
    for (var i = 0; i < Page_ValidationSummaries.length; i++)
            Page_ValidationSummaries[i].style.display = "none";

}
</script>

这篇关于通过javascript重置asp.net验证控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆