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" Validatio开发者_如何转开发nGroup="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 ... 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.
Change "CausesValidation" to False on your ImageButton
精彩评论