1

I want to click a button (ASP.net) - which needs to run a JS method and if its true, then it has to call the ASP.Net - ie., i hv a form, and if its validated(JS returning true) in the client side - then only my asp.net button -click event must fire.

How to do this?

3 Answers 3

3

Try OnClientClick attribute of asp:Button i.e.

<asp:Button ...  OnClientClick="return yourValidationMethod()" />

Make sure that your validation method returns true or false based on the validation.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to run something only on one response, use @Cybernate's example but modified:

<asp:Button ...  OnClientClick="if (yourValidationMethod() == false) return false;" />

If invalid, the return false would block the postback, otherwise normal postback happens.

2 Comments

in what way it is different from the <asp:Button ... OnClientClick="return yourValidationMethod()" /> ?? Just curious to know as im new to JS !
OK, after the client click code, there is either a __doPostBack or WebForm_DoPostBackWithOptions call that actually triggers the postback to the server. Just doing return yourValidationmethod(); will never allow the postback when the validation succeeds. Exiting the routine only when false, as I have, will only block the postback when invalid.
1

Use OnClientClick property for ASP.NET button. Return false if validation failed, return true if it succeeded.

1 Comment

Thanks Pal. I know OnClientClick - but didnt know these days this has a nice effect !! what a lame guy im !!

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.