When you attach a client-side click event via jQuery to an ASP.NET button do you then lose the ability to do a postback? I merely need to do some jQuery prior to the server-side stuff happening and it seems to be interrupting this process.
$('[id$="btnDisableAlarms"]').click(function () {
var commentValue = $('[id$="txtComment"]').val();
if (commentValue == "") {
// add red border (CSS)
$('[id$="txtComment"]').addClass("redCommentError");
}
return;
});
The button is wired up with a click event but the browser doesn't budge.
btnDisableAlarms.Click+=new EventHandler(btnDisableAlarms_Click);
public void btnDisableAlarms_Click(object sender, EventArgs e)
{
Response.Write("Still able to manage a postback from server");
}
The problem is that your jQuery click handler is overwriting the click handler that ASP assigns to the button. You could do something like:
$('[id$="btnDisableAlarms"]').each(function (index, button) {
var oldClickHandler = button.onclick;
$(button).click(function () {
// Do your stuff
oldClickHandler();
});
});
That might not be the right syntax for caching the old click handler, but it would be something similar to that.
Edit: Actually it would be safer to preserve the context and the click event, something like:
$('[id$="btnDisableAlarms"]').each(function (index, button) {
var oldClickHandler = button.onclick;
$(button).click(function (clickEvent) {
// Do your stuff
oldClickHandler.call(button, clickEvent);
});
});
This ensures that if the old click handler uses this or tries to access the click event that triggered it, it'll still work properly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With