通过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>