简体   繁体   中英

Problem with ASP.NET asynchronous calls, waiting for handler to return

I'm trying to find an answer for a problem my developers are having. I don't know this well enough myself...

We are using ASP.NET with C#.

When a user presses a button on a page, we call a hander to save the session variables to the current view state of the form (some IDs that are used).

Then, we call a GreyBox window with other functionality.

Because this is asynchronous, greybox doesn't wait for the handler to respond.

In many cases the greybox is loaded before the session variables are saved to the view state, and in this case, grey box doesn't have the IDs that are necessary.

On the localhost, it is fast enough that we never realized the problem. In production, it is a problem.

What would be the correct solution here?

The asynchronous call to the server to save the session will return a response to the client. Don't start greybox until you have a successful reply from the server. If there is only one thing causing async postbacks on your form, then you can plug into the reply by doing this:

<script type="text/javascript">
    //<![CDATA[
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(endRequest);
    function endRequest(sender, e) {
        // Do stuff
    }
    //]]>
</script>

For more complicated scenarios see the article on MSDN on this subject.

You need to make sure that you're additional code is running in a callback method from your AJAX request.

If you're manually calling a web service or page method the Sys.Net.WebServiceProxy.invoke method takes a callback: http://msdn.microsoft.com/en-au/library/bb383814.aspx

I have a feeling that the PageRequestManager which David suggested only works if you are using an UpdatePanel to perform the AJAX request.

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