开发者

ASP.NET button-click, using jQuery on a server control

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 () {
          开发者_JAVA百科  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.


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).

Read here for examples: http://api.jquery.com/submit/


Just have your JQuery method return true at the end, and it will perform a postback.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜