如何清除按钮单击事件上的 CustomValidator 错误?

发布于 2024-08-30 07:33:38 字数 1388 浏览 3 评论 0原文

我有一个用于输入日期的复合用户控件:

alt text

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:

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

看轻我的陪伴 2024-09-06 07:33:38

尝试在 onClick 调用末尾添加 return false;。这应该可以防止默认行为(在本例中为提交)。

btnClear.Attributes.Add("onClick", "btnClear_OnClick(); return false;")

Try adding a return false; at the end of your onClick call. That should prevent the default behavior (in this case submit).

btnClear.Attributes.Add("onClick", "btnClear_OnClick(); return false;")
故事还在继续 2024-09-06 07:33:38

如果他们无需单击任何正确的内容即可修复错误,您是否想要清除客户端的错误消息?

如果我有你的问题,但调用一个重新验证函数,该函数调用一些客户端 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文