In my scenario i have many asp.net button in the page. Each asp.net button when rendered on client side has a strucutre like the following :
<button onclick="__doPostBack('ctl00$...ID','')" type="button" class=".."></button>
Some of the button in the page have a special class that mean that the button will cause form validation :
<button onclick="__doPostBack('ctl00$...ID','')" type="button" class=".. require-validation"></button>
I would like to execute a javascript function for validate the form before run the regular onclick function only on those buttons. And if possibile i would like change this behaviour only in client side using jQuery (so that server side i have only to tag buttons with the appropiate class).
If i try to target (with jQuery) all the button with the class "require-validation" and attach the validation function (that will suppress the submit event) things works, but only sometimes.
Here the validation function example :
$("myOnlyPageForm").find(".require-validation").click(function (e) {
if ( trikyValidationFail() )
e.preventDefault();
});
My understanding is that the button with "require-validation" has now two different event attached, so a click will fire them both and the order is not guaranteed. So sometimes it prevent the regular postback, but sometimes not (in my test always the first time the postback is prevented, but the second click cause a postback even if the validation always fail).
So till now i only can think to rewrite entirely the onclick code (using jQuery) so it will become :
<button onclick="trikyValidationFail() ?__doPostBack('ctl00$...ID','') : false" type="button" class=".. require-validation"></button>
but i am not sure if this is faseable, nor how to achieve that