Form Validation i stopping OnClick event functioning

Okay so I had this function working fine which basically takes the postcode then looks up the address from code behind and then populates some other text boxes.

But now I have added the validation checks everytime I click the button to fire the onclick event the validation is fired and the onclick function isnt called.

Can you help?

<asp:TextBox CausesValidation="false" CssClass="GlobalFormTextBox" ID="PostCodeTextBox" runat="server" Text='<%# Bind ("Postcode") %>'></asp:TextBox>
<asp:RequiredFieldValidator runat="server"
id="reqPostCode" ControlToValidate="PostCodeTextBox"
ErrorMessage = "PostCode" Text="*"
display="Dynamic" />
<asp:ImageButton ID="CheckPostCode" runat="server" OnClick="CheckPostCodeButton_Click" ImageUrl="~/images/PostCodeLookupIcon.gif" AlternateText="Find Address" />
protected void CheckPostCodeButton_Click(object sender, EventArgs e) 
///some code in here

Open in new window

Who is Participating?
tiagosalgadoConnect With a Mentor Commented:
Your OnClick function only fired when your RequiredFieldValidator returns true.
If you want to execute your OnClick function, even if your control isn't validated, you need to add CausesValidation="false" to your ImageButton control.
Is there a Validation Error, indicating that the textbox is empty?  If the textbox is empty, the OnClick won't fire, as the validation is checked first before even posting back.
andrewmilnerAuthor Commented:
Nice one.  Didnt think to add it to the button only the text box.
Many Thanks.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.