ASP.Net 中的一个功能是能够从客户端代码调用服务器端代码而无需回发,使用称为客户端回调的东西。到目前为止,我发现了一些小警告:-
- 它使用目前只有 IE 的 XmlHttp。 Firefox 和其他浏览器有替代方案,但回调仅使用此方案。
- 您可以从服务器返回的唯一类型是字符串(但如果需要,我们可以通过序列化来解决这个问题)
我使用的示例是我有两个需要保持同步的相关文本框。如果更改了 ClientID 框,则 ClientName 框应显示具有该 ID 的客户端的名称,反之亦然。
要开始使用此功能,请确保您的代码隐藏实现 ICallbackEventHandler 接口:-
public partial class WebForm1 : System.Web.UI.Page, ICallbackEventHandler
接下来,我在我的 aspx.cs 中的 Page_Load 方法中注册我的回调方法:-
// Set up client callbacks. These allow client-side scripts to call
// server-side functions and retrieve the results. Its a string-only
// return I'm afraid, limited by the ICallbackEventHandler method signatures
txtClientID.Attributes.Add("onchange", "GetClientNameById('id|' + this.value, 'id');");
string callBackClientID = Page.ClientScript.GetCallbackEventReference(this, "arg", "ClientNameCallback", "context", "ClientNameCallbackError", true);
string clientIDfunction = "function GetClientNameById(arg,context) { " + callBackClientID + "; }";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "GetClientNameById", clientIDfunction, true);
txtClientName.Attributes.Add("onchange", "GetClientIdByName('name|' + this.value, 'name');");
string callBackClientName = Page.ClientScript.GetCallbackEventReference(this, "arg", "ClientIdCallback", "context", "ClientIdCallbackError", true);
string clientNamefunction = "function GetClientIdByName(arg, context) { " + callBackClientName + "; }";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "GetClientIdByName", clientNamefunction, true);
这会将服务器函数注册到页面并将它们连接到客户端回调方法 - 这些回调方法是基本的占位符,除了给服务器一个返回字符串的地方之外什么都不做。所以在 aspx 页面本身:-
<script type="text/javascript">
function ClientNameCallback(result, context)
{
//sorry about the hardcoded element name...
if(result != "")
document.getElementById('ctl00_ContentPlaceHolder1_txtClientName').setAttribute('value',result);
}
function ClientIdCallback(result,context)
{
//sorry about the hardcoded element name...
if(result != "")
document.getElementById('ctl00_ContentPlaceHolder1_txtClientID').setAttribute('value',result);
}
function ClientNameCallbackError(result, context)
{
//Not sure what error is likely to arise at this point, but...
alert('Error in client name callback function - please say that to eSolutions!');
}
function ClientIdCallbackError(result, context)
{
//Not sure what error is likely to arise at this point, but...
alert('Error in client id callback function - please say that to eSolutions!');
}
</script>
最后,我们实现所需的 ICallbackEventHandler,其中包含我们要执行的服务器端处理:-
string ICallbackEventHandler.GetCallbackResult()
{
return callbackReturnValue;
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
// eventArgument should be in format field|value,
// e.g., "id|30102" or "name|test client"
// This is because of the "single string" limitations
// of the callback interface
if(eventArgument.StartsWith("name"))
{
//....do lookup to get the id based on the name, from an array or database, or whatever
callbackReturnValue = <string we want to pass back to client-side
}
else if(eventArgument.StartsWith("id"))
等等。
等等