1

I would like this ASP button control to stop calling its event handler when the JavaScript client method IsCorrectPrice() returns false.

<asp:Button ID="btnsubmit" runat="server" 
            Text="Submit" OnClientClick="javascript:IsCorrectPrice()"/>


btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) 
  Handles btnsubmit.Click

How can this be done?

2 Answers 2

5

Change your OnClientClick to be like this:

OnClientClick="return IsCorrectPrice();"
Sign up to request clarification or add additional context in comments.

4 Comments

Just return IsCorrectPrice(); is enough
thank you very much p.campbell and Ghostoy, both of your answers work! :)
@Liz: thanks! Be sure to use the green checkmark to 'accept' an answer for all your StackOverflow questions. It's part of the reputation system.
Hello! I just found an issue regarding this. I added a validation group so this what happened, even when the IsCorrectPrice() return true but the validation group has an error, it still posts back. I found out that validationgroup logic comes after the clientscript. So now, i used the original answer of p.campbell - OnClientClick="if(!IsCorrectPrice()) return false;" Thank you all!
0

If you have the option to use jQuery, you could use this..

$("#btnsubmit").click(function() {
   if (!IsCorrectPrice()) {
      e.preventDefault();
   }
});

*Note:

Using the OnClientClick right there in the DOM starts to get unmaintainable after the most basic scenarios.

Also, the #btnsubmit may not be the exact id of your button, but that depends on what version of ASP.Net you are using and if you are using 4, what your ClientIdMode is set to.

1 Comment

Thank you very much ar3, I will keep this in mind whenever I use jQuery. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.