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.
当您通过jQuery将客户端单击事件附加到ASP时。那么你是否失去了做回发的能力?在服务器端出现问题之前,我只需要做一些jQuery操作,这似乎会中断这个过程。
$('[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");
}
3 个解决方案
#1
2
The problem is that your jQuery click handler is overwriting the click handler that ASP assigns to the button. You could do something like:
问题是,jQuery单击处理程序覆盖了ASP为按钮分配的单击处理程序。你可以这样做:
$('[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.
这确保了如果旧的单击处理程序使用这个或尝试访问触发它的单击事件,它仍将正常工作。
#2
0
You could try firing the normal form submission using jQuery's .submit() method (or maybe listening to the submission event rather than the click event).
您可以尝试使用jQuery的.submit()方法来启动常规的表单提交(或者可能是侦听提交事件而不是单击事件)。
Read here for examples: http://api.jquery.com/submit/
请阅读这里的示例:http://api.jquery.com/submit/
#3
0
Just have your JQuery method return true at the end, and it will perform a postback.
只要您的JQuery方法在最后返回true,它就会执行回发。
#1
2
The problem is that your jQuery click handler is overwriting the click handler that ASP assigns to the button. You could do something like:
问题是,jQuery单击处理程序覆盖了ASP为按钮分配的单击处理程序。你可以这样做:
$('[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.
这确保了如果旧的单击处理程序使用这个或尝试访问触发它的单击事件,它仍将正常工作。
#2
0
You could try firing the normal form submission using jQuery's .submit() method (or maybe listening to the submission event rather than the click event).
您可以尝试使用jQuery的.submit()方法来启动常规的表单提交(或者可能是侦听提交事件而不是单击事件)。
Read here for examples: http://api.jquery.com/submit/
请阅读这里的示例:http://api.jquery.com/submit/
#3
0
Just have your JQuery method return true at the end, and it will perform a postback.
只要您的JQuery方法在最后返回true,它就会执行回发。