AJAX技术所提倡的无刷新回调,在原来的技术中需要写大量的JavaScript代码或使用一些AJAX框架,使得开发效率和可维护性大大降低。其实ASP.NET2.0中,已经提供了这样的接口,这就是ICallbackEventHandler。
关于ICallbackEventHandler网上已经有很多文章介绍了,这篇实为画蛇添足。
ICallbackEventHandler存在于System.Web.UI中,我们先做一个非常简单的例子来试用一下。
第一步,在VS2005中建立一个新的WEB窗件。
第二步,在ASPX中,放上一段HTML代码(如下):
<body>
<form id="form1" runat="server">
<div>
<button onclick="CallServer()">CallServer</button>
</div>
</form>
</body>
<body> <form id="form1" runat="server"> <div> <button onclick="CallServer()">CallServer</button> </div> </form> </body>
第三步,然后在<HEAD></HEAD>中放入一段JavaScript脚本:
<script type="text/javascript">
function CallServer()
{
var product = "测试";
<%= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;
}
function ReceiveServerData(rValue)
{
alert(rValue);
}
</script>
第四步,在此ASPX的后台CS代码中,继承ICallbackEventHandler接口,并实现接口中的两个方法:
ICallbackEventHandler.GetCallbackResult()
和
ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
第五步,增加一个变量CallBackValue,并修改接口的两个方法为:
private string CallBackValue = string.Empty;
string ICallbackEventHandler.GetCallbackResult()
{
return CallBackValue + ",ok";
}
void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
{
this.CallBackValue = eventArgument;
}
第六步,运行,界面上会出现一个按钮,点击后,会将“测试”这个字符串传至后台,后台C#代码将字符串加上“,OK”后返回给客户端的JavaScript代码,并显示。
以上六步,就可以实现无刷新回调了。现在,我们来分析一下几段代码。
先看第三步中的JavaScript代码,其中的CallServer()方法中进行了回调,回调的语句为:
<%= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;
里面四个参数中第二个参数指定将product这个JavaScript中的字符串变量传回后台,第三个参数指定了从后台返回时接收返回信息的JavaScript方法ReceiveServerData(string Value)。
第五步中后台的两个方法,一个ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)用来接收前台JavaScript中传来的字符串变量,并赋值给内部变量this.CallBackValue,另一个方法ICallbackEventHandler.GetCallbackResult()将变更后的内部变量this.CallBackValue返回给前台JavaScript方法ReceiveServerData(string Value)。
调用的顺序是: (前台)CallServer() --> (后台)ICallbackEventHandler.RaiseCallbackEvent(string eventArgument) --> (后台)ICallbackEventHandler.GetCallbackResult() --> (前台)ReceiveServerData(string Value)。
整个调用过程非常简单,而其中非常关键的一步是第三步的
<%= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;
这个方法,以下是从网上找来的一段资料,大家可以看看。
GetCallbackEventReference使得客户端方法在客户端请求结束时得到回收。 它也让CallBackManager 确定产生哪种回叫方法。 在这个例子内使用的被重载的方法是:
public string GetCallbackEventReference(
string target, string argument,
string clientCallback, string context,
string clientErrorCallback)
Table 1. GetCallBackEventReference 方法的参数描述。
Parameters Description target ID of the page where the callback invocation is handled. For more see the other overloaded options available in the next immediate section.In our sample "this" is the argument value, since the callback is handled in the same page. argument This is the parameter defintion used to send value to the server. This value is received by parameter "eventArgument" at the server end using the RaiseCallbackEvent event."arg" becomes the first parameter name in our sample. The value is passed through this argument from the client. clientCallback Method name of the callback that is invoked after successful server call."CallBackHandler" is the method name that handles the callback. context A parameter that is associated with the "argument" from the client. It usually should be used to identify the context of the call. You will understand this better from the sample implementation.In the sample "ctx" is just another parameter definition used. The value for this is passed from the client. clientErrorCallback Name of the method that is called from the CallBackManager in case of any errors.
从这个方法返回的string是:
__doCallback('__Page',arg,CallBackHandler,ctx, ErrorCallBack)
另一个重载方法是:
public string GetCallbackEventReference(
Control control, string argument,
string clientCallback, string context)
public string GetCallbackEventReference(
Control control, string argument,
string clientCallback, string context,
string clientErrorCallback)
分享到:
相关推荐
ICallbackEventHandler接口无刷新源码 功能介绍: 在 ASP.NET 网页的默认模型中,用户会与页交互,单击按钮或执行导致回发的一些其他操作。此时将重 新创建页及其控件,并在服务器上运行页代码,且新版本的页被...
ASP.NET基于ICallbackEventHandler的轻量级异步回调 CSDN学生大本营文章代码
ICallbackEventHandler接口实现多级联动
通过实现ICallbackEventHandler接口和借助GetCallbackEventReference生成脚本,完成repeater分页无刷新的效果
ICallBackEventHandler(ajax应用),ajax应用实例。。
关于如何实现ICallbackEventHandler的简单指南
Asp.net无刷新中文验证码的实现代码,V2.0(建议采用VS2005(Sp1)或以上版本开发工具进行开发编译 ) 以前在网上找到了很多关于中文验证码的文章,但是都没有调测成功,总出现 The target '__Page' for the callback ...
利用JavaScript代码调用服务器代码,从服务器端返回html到浏览器,再通过JavaScript添加到页面,实现局部刷新.
Visual studio 2008 ASP.NET 实现IcallbackEventHandler Ajax实现手机号码查询
Thetarget'__Page'forthecallbackcouldnotbefoundordidnotimplementICallbackEventHandler不能ICallbackEventHandler回掉的错误,我进行了一下修正并整理,现在可以实现了中文验证码无刷新的操作,现特把全部源码...
前两天看了一个自定义分页控件,和AspNetPager一样是实现IPostBackEventHandler接口,不过简洁许多,就想能不能实现ICallbackEventHandler接口做到无刷新分页呢?想到了就马上去做,终于,设想变成了现实!!
汇集了多种AJAX实现的方法,包括微软UpdatePannel、实现ICallBackEventHandler的AJAX,基于Prototype的AJAX,手写xmlRequest的AJAX,以及第三方组件的AJAX
ICallbackEventHandlerDemo ,关于Ajax异步提交的例子,没有用到MS提供的Asp.net框架。主要是实现了ICallbackEventHandler接口
通过实现ICallbackEventHandler接口来再请求本页的代码页面,可以用在数据的操作,后台代码可以随便写。 还有一个是最基本的AJAX入门实例,基于url请求的
public class AjaxPager : Control, ICallbackEventHandler, INamingContainer { #region Member Variables private int lowerBound; private int upperBound; private HyperLink previousLink; private ...