如何清除按钮单击事件上的 CustomValidator 错误?
我有一个用于输入日期的复合用户控件:
CustomValidator 将包含服务器端验证代码。如果用户以任何方式更改日期值,我希望通过客户端脚本清除错误消息。为此,我添加了以下代码,将两个下拉列表和年份文本框连接到验证控件:
<script type="text/javascript">
ValidatorHookupControlID("<%= ddlMonth.ClientID%>", document.getElementById("<%= CustomValidator1.ClientID%>"));
ValidatorHookupControlID("<%= ddlDate.ClientID%>", document.getElementById("<%= CustomValidator1.ClientID%>"));
ValidatorHookupControlID("<%= txtYear.ClientID%>", document.getElementById("<%= CustomValidator1.ClientID%>"));
</script>
但是,我还希望当用户单击清除按钮时清除验证错误。当用户单击“清除”按钮时,其他 3 个控件将被重置。为了避免回发,“清除”按钮是一个常规 HTML 按钮,带有一个重置 3 个控件的 OnClick 事件。不幸的是,ValidatorHookupControlID 方法似乎不适用于 HTML 控件,因此我想将 HTML 按钮更改为 ASP 按钮,然后改为 Hookup 到该控件。但是,我似乎无法消除默认情况下与 ASP 按钮控件关联的回发功能。我尝试将 UseSubmitBehavior 设置为 False,但它仍然提交。我尝试在 btnClear_OnClick 客户端代码中返回 false,但发送到浏览器的代码在我的调用后包含 DoPostback 调用。
btnClear.Attributes.Add("onClick", "btnClear_OnClick();")
我没有添加 OnClick 代码,而是尝试覆盖它,但 DoPostBack 代码仍然包含在发送到浏览器的最终代码中。
我需要做什么才能让“清除”按钮清除单击时的 CustomValidator 错误并避免回发?
btnClear.Attributes.Item("onClick") = "btnClear_OnClick();"
I have a composite User control for entering dates:
The CustomValidator will include server sided validation code. I would like the error message to be cleared via client sided script if the user alters teh date value in any way. To do this, I included the following code to hook up the two drop downs and the year text box to the validation control:
<script type="text/javascript">
ValidatorHookupControlID("<%= ddlMonth.ClientID%>", document.getElementById("<%= CustomValidator1.ClientID%>"));
ValidatorHookupControlID("<%= ddlDate.ClientID%>", document.getElementById("<%= CustomValidator1.ClientID%>"));
ValidatorHookupControlID("<%= txtYear.ClientID%>", document.getElementById("<%= CustomValidator1.ClientID%>"));
</script>
However, I would also like the Validation error to be cleared when the user clicks the clear button. When the user clicks the Clear button, the other 3 controls are reset. To avoid a Post back, the Clear button is a regular HTML button with an OnClick event that resets the 3 controls. Unfortunately, the ValidatorHookupControlID method does not seem to work on HTML controls, so I thought to change the HTML Button to an ASP button and to Hookup to that control instead. However, I cannot seem to eliminate the Postback functionality associated by default with the ASP button control. I tried to set the UseSubmitBehavior to False, but it still submits. I tried to return false in my btnClear_OnClick client code, but the code sent to the browser included a DoPostback call after my call.
btnClear.Attributes.Add("onClick", "btnClear_OnClick();")
Instead of adding OnClick code, I tried overwriting it, but the DoPostBack code was still included in the final code that was sent to the browser.
What do I have to do to get the Clear button to clear the CustomValidator error when clicked and avoid a postback?
btnClear.Attributes.Item("onClick") = "btnClear_OnClick();"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试在
onClick
调用末尾添加return false;
。这应该可以防止默认行为(在本例中为提交)。Try adding a
return false;
at the end of youronClick
call. That should prevent the default behavior (in this case submit).如果他们无需单击任何正确的内容即可修复错误,您是否想要清除客户端的错误消息?
如果我有你的问题,但调用一个重新验证函数,该函数调用一些客户端 JavaScript 代码,然后隐藏错误消息所在的范围(如果他们修复了问题),我会执行类似的操作。
请参阅我的博客文章并告诉我这是否是您想要解决的问题。您要阅读的部分位于底部。
http://coding.infoconex.com/post/ASPNET-CustomValidator-that-validates-multiple-controls-using-both-Server-Side-and-Client-Side-scripting.aspx
Are you wanting to clear the error message on the client side if they fix the error without having to click on anything right?
I did something like this if I have your issue right but calling a revalidation function that called some client side javascript code and then hides the span that the error message is in if they fixed the issue.
See my blog article and let me know if this is what you are wanting to solve. The part you want to read is towards the bottom.
http://coding.infoconex.com/post/ASPNET-CustomValidator-that-validates-multiple-controls-using-both-Server-Side-and-Client-Side-scripting.aspx