简体   繁体   中英

How do you register a callback to codebehind that is called when the service call completes?

I'm using a web service with an ASP page. I'd like to keep most of what is going on here happening on the service, but I need to call one function from the codebehind. How do I do this and keep it asynch.?

How do you register a callback to codebehind that is called when the service call completes?

Thanks!!

EDIT: EXTRA INFO: I call an asmx web service using $.ajax from the jQuery library. I'd like to avoid too many changes, but again my end result must be calling a function from the service and upon completion calling a codebehind function, all as asynchronous as possible.

                $.ajax({
                    type: "POST",
                    url: "WebService.asmx/InsertClient",
                    contentType: "application/json; charset=utf-8",
                    data: insertdata,
                    dataType: "json",
                    success: function (msg) {
                        pkey = msg.d;
                        inserted();
                        return false;
                    },
                    error: function (msg) {
                        alert(msg.status + msg);
                        return false;
                    }

                });

All of that happens correctly and everything works, but I'm just having trouble trying to work in an asynchronous call to codebehind - because I need to update a dropdown to refresh its datasource - as here I have just added a new entry.

If you are calling the webservice from JS, you can hookup a OnSuccess event in JS and then do a __doPostback to your page from Javascript.

example:

`function testCall() { WebServiceProxy.GetDocuments(param01, this.onSucceed, this.onFailure); }

function onSucceed (result) { // if result is ok __doPostback(clientID, params); }

function onFailure (result) { }`

In the asp Page/UserControl you need to implement IPostBackEventHandler. For example that way:

    public void RaisePostBackEvent( string eventArgument )
    {
        switch( eventArgument )
        {
            case "CallComplete":
                OnWebServiceCompleted( new WebServiceCompletedEventArgs( value1 ) );

                break;
            default:
                break;
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM