requiredFieldValidator 要求用户单击两次

发布于 2024-11-29 16:29:10 字数 894 浏览 0 评论 0原文

我有一个简单的网络表单,其中有一个文本框和一个连接到它的RequiredFieldValidator。当触发RequiredFieldValidator错误时,用户必须单击提交两次才能发布表单。第一次单击会清除错误,第二次单击实际上会触发按钮单击事件。这是预期的行为吗?

<asp:RequiredFieldValidator ID="reqFieldCloseComment" ControlToValidate="tbCloseComment" ValidationGroup="ChangeStatus" ErrorMessage="Please enter a reason" Display="Dynamic" runat="server"></asp:RequiredFieldValidator>
            <asp:TextBox ID="tbCloseComment" runat="server" CausesValidation="true" TextMode="MultiLine" Height="107px" Width="400px"></asp:TextBox>

        <asp:Button ID="btnCloseRequestFinal" Text="Finish" CssClass="CloseReqButton" runat="server" ValidationGroup="ChangeStatus" />

我尝试根据 Google 搜索中找到的建议将 CausesValidation 添加到文本框,但没有帮助。

编辑似乎并不总是需要双击才能触发事件。只要在文本框中输入文本,然后将焦点从文本框中移开,RequiredFieldValidator 错误消息就会消失,并且表单只需单击一下即可。

I have a simple web form with a textbox and a RequiredFieldValidator wired up to it. When the RequiredFieldValidator error is triggered the user has to click the submit twice to post the form. The first click clears the error, the second actually fires off the button click event. Is this expected behavior?

<asp:RequiredFieldValidator ID="reqFieldCloseComment" ControlToValidate="tbCloseComment" ValidationGroup="ChangeStatus" ErrorMessage="Please enter a reason" Display="Dynamic" runat="server"></asp:RequiredFieldValidator>
            <asp:TextBox ID="tbCloseComment" runat="server" CausesValidation="true" TextMode="MultiLine" Height="107px" Width="400px"></asp:TextBox>

        <asp:Button ID="btnCloseRequestFinal" Text="Finish" CssClass="CloseReqButton" runat="server" ValidationGroup="ChangeStatus" />

I tried adding CausesValidation to the textbox per a suggestion found from a Google search and it doesn't help.

EDIT It seems that it doesn't always have to be a double click to fire off the event. As long as text is entered into the textbox and then the focus is taken away from the textbox, the RequiredFieldValidator error message goes away and the form requires only a single click.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

糖果控 2024-12-06 16:29:10

我在使用 CompareValidator 时遇到了同样的问题,当我将 Display 属性从动态更改为静态。希望有帮助

I had the same issue with a CompareValidator and found the problem went away when I changed the Display property from Dynamic to Static. Hope that helps

一页 2024-12-06 16:29:10

发生这种情况是因为当文本框失去焦点时会运行清除错误消息的代码。那么发生的情况是:

  1. 您在字段中输入文本
  2. 单击按钮,这会导致文本框上发生 onblur 事件,触发代码再次检查字段的值并删除错误消息
  3. 现在验证中没有错误,因此,再次单击该按钮即可提交表单。

当您先按 Tab 键(或者基本上执行任何使焦点离开文本框的操作)时,onblur 脚本就会运行并清除错误,以便当您单击提交按钮时,它就可以开始了。

This happens because the code that clears out the error message runs when the textbox loses focus. So what happens is:

  1. You enter text in the field
  2. You click on the button, which causes the onblur event to happen on the textbox, firing the code to check the field's value again and removing the error message
  3. Now there are no errors in validation, so clicking the button again submits the form.

When you press the tab key first (or basically do anything that takes the focus off the textbox), then that onblur script runs and clears out the error so that when you click the submit button it is ready to go.

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