在看到众多网站转载的这篇文章时,发现调式时有错误,特发表了此修正版
后面的例子大家针对原版举一反三。
Asp.Net2.0的客户端回调是一种很让人激动的方法,他能够让我们控制要提交什么数据给服务器而不用提交整个页面,同时服务器也只返回你所需要的数据而不要发回整个页面。
首先我们要说一个很重要的方法:GetCallbackEventRefernce.
GetCallbackEventReference首先实现让客户端脚本有能力传递参数给服务器端的RaiseCallbackEvent方 法,然后返回RaiseCallBackEvent方法的值给你在GetCallbackEventRefernce方法中注册的一个参数(其实也是一个 你要在客户端写的脚本)。调用GetCallbackEventRefernce你必须从客户端脚本中传递给他两个参数,一个是要传递给 RaiseCallbackEvent事件的值,一个是context.
他的参数意义如下:
第一个:实现了ICallbackEventHandler借口的页面或者服务器控件,写this代表但前页面。
第二个:代表你从要从客户端传递给服务器RaiseCallbackEvent方法的值
第三个:你要在客户端写的一个js函数,同时,服务器也会把计算得到的数据传递给这个函数做为这个函数的参数。
第四个:context具体什么意思我也不太清楚GetCallbackEventRefernce发送到了客户、端的代码是这样的:
WebForm_DoCallback('__Page',arg,ReceiveServerData,context,null,false)
那么我们要怎么样做才能够从客户端调用他呢?看到了三中方法:
第一种:在后台写个public string,在Page_Load中给他赋值为:=Page.ClientScript.GetCallbackEventReference (this, "message", "ShowServerTime", "context");注意在这里是Page.ClientScrip,因为他会返回个ClientScriptManager, ClientScriptManager管理所有的客户端脚本。然后在前台某个按钮的onclick事件里<%=那个public后台字符串% >.做个小实验代码如下:
前台ServerTime.aspx:为了方便去掉好多没用的html
<%
@ page language
=
"
C#
"
CodeFile
=
"
ServerTime.aspx.cs
"
Inherits
=
"
ServerTime_aspx
"
%>
<
html
>
<
head
>
<
title
>
Server Time
</
title
>
<
script language
=
"
javascript
"
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
function GetServerTime()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
var message = '';
var context = '';
<%=sCallBackFunctionInvocation%>
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
function ShowServerTime(timeMessage, context)
...
{
alert('现在服务器上的时间是: ' + timeMessage);
}
</
script
>
</
head
>
<
body
>
<
form id
=
"
MainForm
"
runat
=
"
server
"
>
<
input type
=
"
button
"
value
=
"
得到服务器端时间
"
onclick
=
"
GetServerTime();
"
/>
</
form
>
</
body
>
</
html
>
后台:
using
System;
using
System.Web.UI;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
public
partial
class
ServerTime_aspx : Page,ICallbackEventHandler
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
//一定要实现ICallbackEventHandler借口
public string sCallBackFunctionInvocation;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
void Page_Load(object sender, System.EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
sCallBackFunctionInvocation = Page.ClientScript.GetCallbackEventReference(this, "message", "ShowServerTime", "context");
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
String returnValue;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
string ICallbackEventHandler.GetCallbackResult()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
return returnValue;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
returnValue = DateTime.Now.ToString();
}
}
注意:
这里定义了一个字符串 String returnValue;
在GetCallbackResult 和 RaiseCallbackEvent 前都加上了 ICallbackEventHandler,好象是C#就要加上这个,VB不用。
一个 string GetCallbackResult 和 void RaiseCallbackEvent 搞定!
--------------------------------------
再给大家一个例子:
前台:
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
CodeFile
=
"
Exp2.aspx.cs
"
Inherits
=
"
Exp2
"
%>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
<!
DOCTYPE html PUBLIC
"
-//W3C//DTD XHTML
1.1
//
EN" "
http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd
">
<
html xmlns
=
"
http://www.w3.org/1999/xhtml
"
>
<
head id
=
"
Head1
"
runat
=
"
server
"
>
<
script type
=
"
text/javascript
"
>
function LookUpStock()
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
var lb = document.forms[0].ListBox1;
var product = lb.options[lb.selectedIndex].text
CallServer(product, "");
}
function ReceiveServerData(rValue)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
Results.innerText = rValue;
}
</
script
>
</
head
>
<
body
>
<
form id
=
"
form1
"
runat
=
"
server
"
>
<
div
>
<
asp:ListBox ID
=
"
ListBox1
"
Runat
=
"
server
"
></
asp:ListBox
>
<
br
/>
<
br
/>
<
button onclick
=
"
LookUpStock()
"
>
Look Up Stock
</
button
>
<
br
/>
<
br
/>
Items
in
stock:
<
span ID
=
"
Results
"
></
span
>
<
br
/>
</
div
>
</
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;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
public
partial
class
Exp2 : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
protected System.Collections.Specialized.ListDictionary catalog;
protected void Page_Load(object sender, EventArgs e)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
String cbReference =
Page.ClientScript.GetCallbackEventReference(this,
"arg", "ReceiveServerData", "context");
String callbackScript;
callbackScript = "function CallServer(arg, context)" +
"{ " + cbReference + "} ;";
Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
"CallServer", callbackScript, true);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
catalog = new System.Collections.Specialized.ListDictionary();
catalog.Add("monitor", 12);
catalog.Add("laptop", 10);
catalog.Add("keyboard", 23);
catalog.Add("mouse", 17);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
ListBox1.DataSource = catalog;
ListBox1.DataTextField = "key";
ListBox1.DataBind();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
string earg = "";
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//#region ICallbackEventHandler 成员
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public string GetCallbackResult()
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
String returnValue;
if (catalog[earg] == null)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
returnValue = "-1";
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
returnValue = catalog[earg].ToString();
}
return returnValue;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
public void RaiseCallbackEvent(string eventArgument)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//throw new Exception("The method or operation is not implemented.");
earg = eventArgument;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1106261