I have an ASP:Button
on a page. It renders as HTML like so:
<input type="submit" name="myButton" value="Do something"
onclick="javascript:WebForm_DoPostBackWithOptions(
new WebForm_PostBackOptions('myButton', '', true, '', '', false, false))"
id="myButton" />
Now I am trying to prevent the default behavior of this button (submitting the form) via a jQuery event handler:
$('#myButton').click(function () {
// do some client-side stuff
if (someCondition) {
// don't postback!
return false;
}
});
The problem here is that the inline click handler that ASP sticks on the input seems to be executing before the jQuery event handler, every time.
The easiest solution here would be to use a plain input type="button"
instead of an ASP:Button
, unfortunately this is not a possibilty as this ASP:Button
does many other things that are required in this scenario.
What would be the best way to prevent the form submission from happening? I've thought of using string manipulation to prepend my handler on the element's onclick
attribute, but this seems really dirty. I'd also like to avoid using the OnClientClick
as this would require my handler function to be publicly exposed. Is there a better way?