通过 JavaScript 重置 ASP.NET 验证控件?
如何通过 JavaScript 重置 asp.net 验证控件?当前代码示例清除错误消息文本,但不会重置下一次表单提交的验证控件。
var cv= document.getElementById("<%= MyValidationContorl.ClientID %>");
cv.innerHTML = '';
更新:
这是表单的完整代码示例。我似乎无法在另一个表单提交时触发验证控件:
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>
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 = '';
Update:
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
每次您发帖时都会触发页面验证,问题似乎是您正在清除验证器内容
cv.innerHTML = '';
,这样您的验证器消息就会永远丢失,并且您会认为验证不会再次触发。对于@Glennular答案,代码不处理验证器
Display
属性,如果将其设置为Dynamic
,则将使用validator.style.display< 切换验证器/code>,但如果将其设置为
None
或Inline
则将使用validator.style.visibility
属性。最好使用 asp.net
ValidatorUpdateDisplay
代替,更新:重置验证摘要
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.and for @Glennular answer, the code does not handle the validator
Display
property, if its set toDynamic
the validator will be toggled usingvalidator.style.display
, but if its set toNone
orInline
thenvalidator.style.visibility
property will be used instead.Its better to use asp.net
ValidatorUpdateDisplay
instead,Update : Reset Validation Summaries
此操作会重置所有验证组中的所有验证器。
This one resets all validators in all validation groups.
尝试以下代码块:
我希望这对我有用。 :)
Try the following chunk of code :
I hope this will work as it worked for me. :)
以下是重置所有验证器
或单个验证器的代码:
Here's code to reset all validators
or a single one:
使用
Page_Validators[i].style.visibility = 'hidden';
不适合我,所以我使用这行代码:
Page_Validators[i].enabled = false;
Using the
Page_Validators[i].style.visibility = 'hidden';
Don't work for me so I use this line of code instead:
Page_Validators[i].enabled = false;