ASP.NET异步调用问题,等待处理程序返回

时间:2021-12-17 13:22:34

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#.

我们在C#中使用ASP.NET。

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).

当用户按下页面上的按钮时,我们调用hander将会话变量保存到表单的当前视图状态(使用的某些ID)。

Then, we call a GreyBox window with other functionality.

然后,我们调用具有其他功能的GreyBox窗口。

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

因为这是异步的,所以greybox不会等待处理程序响应。

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.

在许多情况下,会话变量保存到视图状态之前会加载greybox,在这种情况下,灰色框没有必要的ID。

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?

这里有什么正确的解决方案?

2 个解决方案

#1


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:

对服务器的异步调用以保存会话将返回对客户端的响应。在服务器成功回复之前,请不要启动greybox。如果表单上只有一个导致异步回发的内容,那么您可以通过执行以下操作来插入回复:

<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.

有关更复杂的方案,请参阅MSDN上有关此主题的文章。

#2


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

您需要确保从AJAX请求中使用回调方法运行其他代码。

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

如果您手动调用Web服务或页面方法,Sys.Net.WebServiceProxy.invoke方法将进行回调: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.

我觉得David建议的PageRequestManager只有在你使用UpdatePanel来执行AJAX请求时才有效。

#1


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:

对服务器的异步调用以保存会话将返回对客户端的响应。在服务器成功回复之前,请不要启动greybox。如果表单上只有一个导致异步回发的内容,那么您可以通过执行以下操作来插入回复:

<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.

有关更复杂的方案,请参阅MSDN上有关此主题的文章。

#2


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

您需要确保从AJAX请求中使用回调方法运行其他代码。

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

如果您手动调用Web服务或页面方法,Sys.Net.WebServiceProxy.invoke方法将进行回调: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.

我觉得David建议的PageRequestManager只有在你使用UpdatePanel来执行AJAX请求时才有效。