必填字段验证器停止图像按钮 onclick?

发布于 2024-11-17 16:36:00 字数 1086 浏览 5 评论 0原文

我构建的图像按钮有问题。当我将必需的字段验证器附加到页面时,它们会阻止按钮 onclick 事件触发。我对此感到非常困惑,因为我在代码中看不到任何问题!

请你帮我看看这个问题好吗?

干杯

<asp:TextBox ID="TB_Newsletter" runat="server" CssClass="nwsltr-input"></asp:TextBox>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator1"  ValidationGroup="V1" runat="server" Display="Dynamic"  ControlToValidate="TB_Newsletter" ErrorMessage="You must enter your email address"></asp:RequiredFieldValidator>

        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" 
           runat="server" ValidationGroup="V1" Display="Dynamic"
           ValidationExpression="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$" 
           ErrorMessage="Invalid Email Address" ControlToValidate="TB_Newsletter"></asp:RegularExpressionValidator>

      <asp:ImageButton ID="IB_SubScri" 
           ImageUrl="~/_includes/images/buttons/nwsltr-btn.png" runat="server" 
           onclick="IB_SubScri_Click" CausesValidation="True" ValidationGroup="V1"/>

I'm having an issue with an image button that I have built. When I attach required field validators to the page, they stop the button onclick event from firing. I am pretty perplexed by this as I can't see any issues in my code!

Please could you cast your eyes over this and help me out?

Cheers

<asp:TextBox ID="TB_Newsletter" runat="server" CssClass="nwsltr-input"></asp:TextBox>

        <asp:RequiredFieldValidator ID="RequiredFieldValidator1"  ValidationGroup="V1" runat="server" Display="Dynamic"  ControlToValidate="TB_Newsletter" ErrorMessage="You must enter your email address"></asp:RequiredFieldValidator>

        <asp:RegularExpressionValidator ID="RegularExpressionValidator1" 
           runat="server" ValidationGroup="V1" Display="Dynamic"
           ValidationExpression="^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$" 
           ErrorMessage="Invalid Email Address" ControlToValidate="TB_Newsletter"></asp:RegularExpressionValidator>

      <asp:ImageButton ID="IB_SubScri" 
           ImageUrl="~/_includes/images/buttons/nwsltr-btn.png" runat="server" 
           onclick="IB_SubScri_Click" CausesValidation="True" ValidationGroup="V1"/>

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

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

发布评论

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

评论(2

薯片软お妹 2024-11-24 16:36:00

当您按下按钮时,它会提交表单,但在此之前,字段验证器将由脚本触发 - 如果验证失败,表单将不会发布。 类型允许您在按下它们时禁用验证:

<asp:ImageButton ... CausesValidation="False"/>

来自 MSDN 上的 CausesValidation属性:

如果控件引起验证则为 true
在任何控件上执行
收到时需要验证
重点;否则为假。默认
是真的。

有关详细信息,请参阅此 MSDN 参考

显然,我们在这里假设按下此按钮时不需要触发验证器。

When you press the button it submits the form, but prior to that the field validators are firing by script - the form won't post if validation fails. <asp:Imagebutton /> and <asp:Button /> types allow you to disable validation when they are pressed:

<asp:ImageButton ... CausesValidation="False"/>

From MSDN on the CausesValidation property:

true if the control causes validation
to be performed on any controls
requiring validation when it receives
focus; otherwise, false. The default
is true.

See this MSDN reference for more information.

Obviously, we assume here that the validator firing when pressing this button is not required.

兮子 2024-11-24 16:36:00

将 ImageButton 上的“CausesValidation”更改为 False

Change "CausesValidation" to False on your ImageButton

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