异步回调
ASP.NET的异步回调(CallBack)
参考: ASP.NET高级编程第四版 P103-P112
相关文章: http://www.dudo.org/article.asp?id=2
//Default.aspx
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="JS_CALLBACK_Default" %>
<!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>JavaScript Call Back</title>
<script language="javascript" type="text/javascript">
// <!CDATA[
function Button1_onclick() {
var strZipCode = document.getElementById("<%=TextBox2.ClientID%>").value;
UseCallBack(strZipCode,"Test");
}
function GetRandomNumberFromServer(TextBox1,context){
document.getElementById("TextBox1").value = TextBox1;
alert(TextBox1.toString());
alert(context.toString());
}
function Button2_onclick(self) {
var strZipCode = document.getElementById("<%=TextBox2.ClientID%>").value;
UseCallBack2(strZipCode,self);
}
function GetRandomNumberFromServer2(TextBox3,context){
document.getElementById("TextBox3").value = TextBox3;
alert(TextBox3.toString());
alert(context.type);
}
// ]]>
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><BR/><BR/>
<input id="Button1" type="button" value="Get Random Number" onclick="return Button1_onclick()" /><br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<hr />
<br />
<input id="Button2" type="button" value="Call Back2" onclick="return Button2_onclick(this)" /><br />
<br />
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</div>
</form>
</body>
</html>
//Default.aspx.cs
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class JS_CALLBACK_Default : System.Web.UI.Page,
System.Web.UI.ICallbackEventHandler
{
private string _callbackResult = null;
protected void Page_Load(object sender, EventArgs e)
{
string cbReference = Page.ClientScript.GetCallbackEventReference(
this, "arg", "GetRandomNumberFromServer", "context");
string cbReference2 = Page.ClientScript.GetCallbackEventReference(
this, "arg", "GetRandomNumberFromServer2", "context");
string cbScript = "function UseCallBack(arg,context)" +
"{" + cbReference + ";" + "}";
string cbScript2 = "function UseCallBack2(arg,context)" +
"{" + cbReference2 + ";" + "}";
Page.ClientScript.RegisterClientScriptBlock(
this.GetType(), "UseCallBack", cbScript, true);
Page.ClientScript.RegisterClientScriptBlock(
this.GetType(), "UseCallBack2", cbScript2, true);
}
// 概要:
// コントロールを対象とするコールバック イベントの結果を返します。
//
// 戻り値:
// コールバックの結果。
public string GetCallbackResult()
{
return _callbackResult;
}
//
// 概要:
// コントロールを対象とするコールバック イベントを処理します。
//
// パラメータ:
// eventArgument:
// イベント ハンドラに渡されるイベント引数を表す文字列。
public void RaiseCallbackEvent(string eventArgument)
{
//Random rnd = new Random();
_callbackResult = eventArgument;
}
}
解释说明:
1、必须实现接口“System.Web.UI.ICallbackEventHandler”并完成他的两个方法
2、public string GetCallbackResult();//放回给页面的值
3、public void RaiseCallbackEvent(string eventArgument);//从页面取值
4、 注册页面回调函数
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
protected void Page_Load(object sender, EventArgs e)
{
string cbReference = Page.ClientScript.GetCallbackEventReference(
this, "arg", "GetRandomNumberFromServer", "context");
string cbScript = "function UseCallBack(arg,context)" +
"{" + cbReference + ";" + "}";
Page.ClientScript.RegisterClientScriptBlock(
this.GetType(), "UseCallBack", cbScript, true);
}
5、完成页面回调处理函数
![](/Images/OutliningIndicators/ContractedBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
function GetRandomNumberFromServer(TextBox1,context){
document.getElementById("TextBox1").value = TextBox1;
alert(TextBox1.toString());
alert(context.toString());
}