У меня есть веб-форма ASP.NET 3.5, которая использует метод фреймворка Page.ClientScript.GetCallbackEventReference (), и я бы хотел, чтобы некоторые из вызовов были синхронными.
В документации сказано, что этим управляет 5-й параметр (см. Ниже). В частности, когда вы передаете false, это должен быть неасинхронный вызов. Однако независимо от того, истинно это или ложно, он по-прежнему обрабатывает вызов асинхронно.
Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context",false);
Есть ли способ обойти это или, возможно, я что-то делаю не так?





Страница ASPX
<%@ Page Language = "VB" AutoEventWireup = "false" CodeFile = "How-to-use-GetCallbackEventReference.aspx.vb" Inherits = "How_to_use_Callback" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml">
<head runat = "server">
<title>How to use GetCallbackEventReference</title>
<script type = "text/javascript">
function GetNumber() {
UseCallback();
}
function GetRandomNumberFromServer(txtGetNumber, context) {
document.forms[0].txtGetNumber.value = txtGetNumber
}
</script>
</head>
<body>
<form id = "form1" runat = "server">
<div>
<input id = "Button1" type = "button" value = "Get Random Number" onclick = "GetNumber()" /><br /><br />
<asp:TextBox ID = "txtGetNumber" runat = "server"></asp:TextBox> </div>
</form>
</body>
</html>
Код позади
Partial Class How_to_use_Callback
Inherits System.Web.UI.Page
Implements System.Web.UI.ICallbackEventHandler
Dim CallbackResult As String = Nothing
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim cbReference As String = Page.ClientScript.GetCallbackEventReference(Me, "arg", "GetRandomNumberFromServer", "context")
Dim cbScript As String = "function UseCallback(arg,context)" & "{" & cbReference & " ; " & "}"
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "UseCallback", cbScript, True)
End Sub
Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
Return CallbackResult
End Function
Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent
CallbackResult = Rnd().ToString()
End Sub
End Class
Для других бедняков, все еще использующих библиотеку MS AJAX, я нашел следующий пост:
Последний комментарий источника MS гласит:
This is actually by design. In order not to block the UI of the browser, this parameter doesn't actually do the request synchronously but makes sure the requests are queued and only one is going on at any given time. The effect is pretty much the same, except that the end-user can still use the browser UI while the request is going on and he won't have to kill the process if the server fails to respond or the network connection falls.
Страница MSDN подтверждает это:
When sending data synchronously in a callback scenario, synchronous callbacks return immediately and do not block the browser. No two synchronous callbacks callback can execute at the same time in the browser. If a second synchronous callback is fired while one is currently pending, the second synchronous callback cancels the first and only the second callback will return.