必填字段验证器停止图像按钮 onclick?
我构建的图像按钮有问题。当我将必需的字段验证器附加到页面时,它们会阻止按钮 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您按下按钮时,它会提交表单,但在此之前,字段验证器将由脚本触发 - 如果验证失败,表单将不会发布。
和
类型允许您在按下它们时禁用验证:来自 MSDN 上的
CausesValidation属性:
有关详细信息,请参阅此 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:From MSDN on the
CausesValidation
property:See this MSDN reference for more information.
Obviously, we assume here that the validator firing when pressing this button is not required.
将 ImageButton 上的“CausesValidation”更改为 False
Change "CausesValidation" to False on your ImageButton