views:

27

answers:

1

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.

+1  A: 

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;
        }
    }
ovm
That's my bad i didn't specify much. I'm currently using jQuery's $.ajax to call an asmx file, I think that's a little different. Also, since I'd like it to be asynchronous isn't calling a postback not asynchronous?
jphenow
I dont think that it's too different. You can equally specify a function or anonymous block of code to be executed when the webservice call succeeds.You could create a PageMethod and call it through JS when the 1st call to the webservice succeeds. That way you can simulate a asynchronous call i guess.
ovm
The problem I ran into while using a pagemethod is that, because its static(shared), I have trouble accessing the object I need access to - because I need to call a DataBind() on it.
jphenow
I guess that if you code too much around the default asp.net lifecycle, you have to implement more in JS. So eg you could make use of http://beebole.com/pure/ to populate a DropDownList with Data returning from the 1st websevice as callback result.I would consider using a default UpdatePanel for the InsertClient action. Or try to put the DropDownList in a UpdatePanel and refresh that panel through Javascript. But either way you'll get a full page life cycle in codebehind, whether hidden by a UpdatePanel refresh or a full postback. Why do you want to use a webservice for the Insert action?
ovm
A few reasons. Primarily because I more fully understand how javascript and web services work, but as you can see I rather need this one to call codebehind. Also I'm trying really hard to avoid a refresh until EVERYTHING that happens on this form is done so that won't be happening for awhile. I find it more difficult to do an ASP.net AJAXy style while sticking close to codebehind.
jphenow
I'll see if I can work with an update panel, I did a little earlier but I think I missed some of the ideas I had with it.
jphenow