[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 418
  • Last Modified:

asp.net 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

1 Solution
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.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now