0

jQuery newbie question here... I have a jQuery dialog that displays a warning with a "OK" or "Cancel" and based on what they click the result needs to then execute the server side ASP onClick event.

I've attempted to write it along these lines:

            "OK": function () {
                $(this).dialog("close");
                return true;
            },
            Cancel: function () {
                $(this).dialog("close");
                return false;

But it never posts back to the asp server side method.

Am I off base in what I am trying to accomplish here, and is there standard 'best practice' type of way of implementing this type of functionality?

3 Answers 3

1

You have to call __doPostBack() method with appropriate eventTarget and eventArgument to call its sever click handler.

  "OK": function () {
       $(this).dialog("close");
       __doPostBack('serverElementId', 'arguments');
   }

You can take a look at the below link to understand how ASP.Net postback mechanism works.

http://wiki.asp.net/page.aspx/1082/dopostback-function/

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

Comments

0

Assuming you want to execute the POST on "OK", you'd need to add something like:

"OK": function () {
  $.post('someurl.asp', {data}, function() {
    // what to do after the post
  }
  $(this).dialog("close");
  return true;
}

As it currently stands, your code just closes the dialog either way.

Comments

0

I don't have experience with asp.net but you can use an AJAX request to post data to your server-side script:

        "OK": function () {
            $(this).dialog("close");
            $.ajax({
                url      : 'myscript.asp',
                type     : 'get',
                dataType : 'json',
                data     : { state : 'OK' },
                success  : function (serverResponse) {
                    //you can now access the serverResponse
                },
                error    : (jqXHR, errorText, errorThrown) {
                    //always make sure to handle your errors
                }
            });
            return true;
        },
        Cancel: function () {
            $(this).dialog("close");
            $.ajax({
                url      : 'myscript.asp',
                type     : 'get',
                dataType : 'json',
                data     : { state : 'Cancel' },
                success  : function (serverResponse) {
                    //you can now access the serverResponse
                },
                error    : (jqXHR, errorText, errorThrown) {
                    //always make sure to handle your errors
                }
            });
            return false;
        }

Docs for $.ajax(): http://api.jquery.com/jquery.ajax

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.