动态网站制作指南 [  QQ表情  ]
[ 投票调查 ]
[ 企业邮箱 ]
[ 网站空间 ]
网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
ASP源码 | .Net源码 | PHP源码 | JSP源码 | JAVA源码 | CGI源码 | VB源码 | C++源码 | Delphi源码 | PB源码 | VF源码 | 汇编 | 服务器
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > ASP.NET技巧
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
文章搜索服务
邮件订阅
输入你的邮件地址,
你将不会错过任何关于:
[ ASP.NET技巧 ]的信息

本月文章推荐
.关于ASP.NET2.0编写扩展存储过程.
.asp.net中大结果集的分页[翻译].
..Net 2.0 原汁原味读取注册表.
.ACCESS的ASP.NET中 如果 数据库操.
.用ASP.Net(C#)连接Oracle数据库的.
.ASP.NET WEB页面多语言支持解决方.
.彻底放弃IIS让Apache也支持ASP.N.
.ASP.NET 2.0发送电子邮件中存在的.
.何时.NET中AppDomain会回收? .
.asp.net开发wap必备:更好的匹配.
..NET扫描远程计算机注册表.
.asp.net 2.0 上传控件的使用.
.关闭按钮点击事件的捕捉 .
.ASP.NET系统用户权限设计与实现.
.在ASP.NET中值得注意的两个地方.
.ASP.NET 无法确保在注册的 JavaS.
.URL重写可删节日期模式---正则表.
..net中大数处理的一些算法思想.
.简写的通用数据层代码.
.用ASP.NET2.0在数据库中存储二进.

六步使用ICallbackEventHandler实现无刷新回调

发表日期:2006-6-9 |


    AJAX技术所提倡的无刷新回调,在原来的技术中需要写大量的JavaScript代码或使用一些AJAX框架,使得开发效率和可维护性大大降低。其实ASP.NET2.0中,已经提供了这样的接口,这就是ICallbackEventHandler。
    关于ICallbackEventHandler网上已经有很多文章介绍了,这篇实为画蛇添足。

ICallbackEventHandler存在于System.Web.UI中,我们先做一个非常简单的例子来试用一下。

   第一步,在VS2005中建立一个新的WEB窗件。
   第二步,在ASPX中,放上一段HTML代码(如下):


1<body>
2    <form id="form1" runat="server">
3    <div>
4        <button onclick="CallServer()">CallServer</button>
5    </div>
6    </form>
7</body>

   第三步,然后在<HEAD></HEAD>中放入一段JavaScript脚本:


 1 <script type="text/javascript">
 2     function CallServer()
 3     {
 4         var product = "测试";
 5         <%= ClientScript.GetCallbackEventReference(this, "product", "ReceiveServerData",null)%>;
 6     }
 7    
 8     function ReceiveServerData(rValue)
 9     {
10         alert(rValue);
11     }
12 </script>
 

   第四步,在此ASPX的后台CS代码中,继承ICallbackEventHandler接口,并实现接口中的两个方法:
 ICallbackEventHandler.GetCallbackResult()
    和
 ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)

   第五步,增加一个变量CallBackValue,并修改接口的两个方法为:


 1 private string CallBackValue = string.Empty;
 2   
 3 string ICallbackEventHandler.GetCallbackResult()
 4 {
 5  return CallBackValue + ",ok";
 6 }
 7
 8 void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
 9 {
10  this.CallBackValue = eventArgument;
11 }
12
 

    第六步,运行,界面上会出现一个按钮,点击后,会将“测试”这个字符串传至后台,后台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)

上一篇:开发ASP.NET Atlas服务器端Extender控件—编写服务器端Extender & Dflying近期动向 人气:5427
下一篇:开发基于ASP.NET WebService的图片验证码服务 人气:6919
浏览全部ICallbackEventHandler的内容 Dreamweaver插件下载 常用网页广告代码全集
  最新网站源码 最新软件下载
2008-10-13 爬爬思特新闻管理系统 v2.0 Beta1
2008-10-13 Pligg v9.9.5 Beta
2008-10-13 广优邮件发送系统 v2.1
2008-10-13 缤纷互动视频交友 v3.1 RC
2008-10-13 MyShop网络商城 build 081005
2008-10-13 Chyrp 超轻量级开源博客引擎 v2.
2008-10-13 162100静态(论坛/文章)系统 v2.4
2008-10-13 金博人才招聘求职网黄金版 v4.2
2008-10-13 愚人笔记 v4.0
2008-10-11 联系人分组工具 v1.1 中文破解版
2008-10-11 FaceMelter变脸 v2.0 汉化破解版
2008-10-11 PathTracker道路跟踪仪 v1.2 破解
2008-10-11 Rooms手机聊天室 v0.6.7 破解版
2008-10-11 RemoteDesktop远程桌面 v1.0 破解
2008-10-11 ProRemote远程调音台 v1.0.1 破解
2008-10-11 PicShare照片共享 v1.0.0 破解版
2008-10-11 Photogene照片编辑器 v1.5 汉化破
2008-10-11 WriteRoom共享文档 v1.0 破解版
  发表评论
姓 名: 验证码:
内 容:
站长工具:网站收录查询 | Google PR查询 | ALEXA排名查询 | CSS在线编辑器 | 广告代码 | js/vbs加密 | md5加密 | 进制转换 | UTF-8 转换工具 | Html转换js | Html转换asp | Html转换php | Html转换perl
实用工具:汉字翻译拼音 | 拼音字典 | 符号对照表 | 个税计算 | 实时汇率查询换算 | 经典小工具 | 汉字简繁转换 | 普通单位换算 | 公制单位换算 | 生辰老黄历 | 国内电话区号 | 国家代码与域名缩写 | 文字加密解密 | 健康查询 | 万年历 | 汉字横竖排版 | 手机号码查询 | 计算器 | ip搜索
业务联系 | 广告刊登 | 频道合作 | 投稿荐稿 | 联系方式 | 加入收藏 | RSS订阅
Copyright © 2000-2008 www.knowsky.com All rights reserved | 网络实名:动态网站制作指南 | 沪ICP备05001343号
ホームページ制作 不動産検索システム 求人情報
防水工事·改修工事 フットサル大会 探偵
SEO対策 中国語教室 ホームページ作成