3

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");
    }

3 Answers 3

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:

$('[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.

Sign up to request clarification or add additional context in comments.

1 Comment

small typo: onClick -> onclick. Too small an edit to fix in the post (edits must be 6+ characters).
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).

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.