Solved Form Validation i stopping OnClick event functioning

Posted on 2009-02-21
Last Modified: 2012-05-06
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

Question by:andrewmilner
    LVL 8

    Accepted 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.
    LVL 12

    Expert Comment

    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.

    Author Closing Comment

    Nice one.  Didnt think to add it to the button only the text box.
    Many Thanks.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now