Reputation: 35
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?
Upvotes: 1
Views: 3881
Reputation: 50728
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.
Upvotes: 1
Reputation: 82943
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.
Upvotes: 3
Reputation: 11266
Use OnClientClick property for ASP.NET button. Return false if validation failed, return true if it succeeded.
Upvotes: 1