Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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"/>
like image 479
Callum Avatar asked Dec 04 '25 21:12

Callum


1 Answers

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.

like image 68
Grant Thomas Avatar answered Dec 06 '25 12:12

Grant Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!