回调的过程如下所示:
1)客户端发出请求,请求内容包括:指定更新控件和传递参数。
2)服务端响应、处理。
3)服务端将处理后的内容以字符串返回。
4)客户端使用一个函数接受返回值
5)客户端更新指定控件。
创建实现客户端回调的 ASP.NET 页,必须实现ICallbackEventHandler接口。
先声明该接口,方法如下:
public partial class CallBackExample : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
{… …}
实现ICallbackEventHandler接口的两个固定名称的方法, RaiseCallbackEvent(eventArgment)和GetCallbackResult()。RaiseCallbackEvent()方法是回调执行的方法,在这个方法中处理回调的内容,它没有返回值,它从浏览器接受一个字符串作为事件参数,即该方法接受客户端java script使传递的参数,注意它是首先触发的。接下来触发的就是GetCallbackResult()方法,它将所得到的结果传回给客户端的java script,java script再将结果更新到页面。
客户端使用ClientScript.GetCallbackEventReference()方法实现回调。这个方法用几个参数其中一个指定关联上下文,一个指定返回客户端的函数名称。
Public string GetCallbackEventReference (Control control,string argument,string clientCallback,string context)
参数与返回值说明:
参数 作用
control 处理客户端回调的服务器 Control。该控件必须实现 ICallbackEventHandler 接口并提供 RaiseCallbackEvent 方法。
argument 从客户端脚本传递一个参数到服务器端的RaiseCallbackEvent 方法。
clientCallback 一个客户端事件处理程序的名称,该处理程序接收服务器端事件返回的结果。
context 启动回调之前在客户端的客户端脚本信息。脚本的结果传回给客户端事件处理程序。
返回值 调用客户端回调的客户端函数的名称。
=================================================================================
前台:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestCallBack.aspx.cs" Inherits="TestCallBack" %>
<!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 id="Head1" runat="server">
<title>无标题页</title>
<script type="text/javascript">
function a() {
var arg = document.getElementById('TextBox1').value + "/" + document.getElementById('TextBox2').value;
CallServer(arg);
}
function ReceiveServerData(arg)
{
document.getElementById("Label1").innerText = arg;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<input id="Button2" οnclick="a()" type="button" value="button" /></div>
<br />
<asp:Label ID="Label1" runat="server"></asp:Label><br />
<asp:Label ID="Label2" runat="server"></asp:Label><br />
</form>
</body>
</html>
后台:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class TestCallBack : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
protected string returnValue;
protected void Page_Load(object sender, EventArgs e)
{
string cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "");
string callbackScript;
callbackScript = "function CallServer(arg){" + cbReference + "};";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);
}
public void RaiseCallbackEvent(string eventArgument)
{
string[] value1 =eventArgument.Split('/');
int num1 =Convert.ToInt16( value1[0]);
int num2 =Convert.ToInt16( value1[1]);
int num3 = num1 + num2;
returnValue = "这是从服务器返回值:" + num3;
}
public String GetCallbackResult()
{
return returnValue;
}
}
---------------------------------------------------------------------------------------------------------------------------------------------