简体   繁体   English

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

[英]Required field validator stopping image button onclick?

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.当我将必填字段验证器附加到页面时,它们会阻止按钮 onclick 事件触发。 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"/>

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 /><asp:Button />类型允许您在按下它们时禁用验证:

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

From MSDN on the CausesValidation property:从 MSDN 上的CausesValidation属性:

true if the control causes validation to be performed on any controls requiring validation when it receives focus;如果控件导致在接收焦点时对任何需要验证的控件执行验证,则为 true; otherwise, false.否则为假。 The default is true.默认值为真。

See this MSDN reference for more information.有关详细信息,请参阅此MSDN 参考

Obviously, we assume here that the validator firing when pressing this button is not required.显然,我们在这里假设按下此按钮时验证器不需要触发。

Change "CausesValidation" to False on your ImageButton将 ImageButton 上的“CausesValidation”更改为 False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM