电信主站 网通分站
购买流程 付款方式 常见问题 在线提问 续租服务 购物车
用户名: 密 码: 忘记密码?
首 页
域名注册
虚拟主机
双线主机
服务器租用
VPS主机
企业邮局
代理专区
客服中心
虚拟主机行业资讯 虚拟主机评测对比 互联网最新动态 技术学院 站长资讯 在线教程 网站运营
搜索优化 服务器 网络编程 图形图象 站长之家 网页制作 操作系统
冲浪宝典 软件教学 视频通信 办公软件 邮件系统 网络安全 认证考试
您当前位置:西部数码->资讯中心-> 在线教程-> .NET
六步使用ICallbackEventHandler实现无刷新回调-.NET教程,评论及其它
作者:网友供稿 点击:3
  西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!虚拟主机可在线rar解压,自动数据恢复设置虚拟目录等.虚拟主机免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金!
文章页数:[1] 
   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)

转自:动态网站制作指南 | www.knowsky.com

文章整理:西部数码--专业提供域名注册虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!
相关主题
文章页数:[1] 
Google
热门文章
·经典收藏之 - C++内存管理详解-.NET教程,C#语言
·Master Page 初探-.NET教程,评论及其它
·GDI+编程10个基本技巧-.NET教程,评论及其它
·VB.NET中让Textbox只能输入数字(二)-.NET教程,VB.Net语言
·stl应用小问题-.NET教程,评论及其它
·WIN32中颜色值(COLORREF)与.NET中颜色值(Color)的转换-ASP教程,系统相关
·打造自己的专业图像工具-Visual C++ 2005图像编程系列【三】-.NET教程,C#语言
·.Net中常见问题及解决方法归类-.NET教程,.NET Framework
·Lex和Yacc从入门到精通(3)--一个极其简单的lex和yacc程序-.NET教程,评论及其它
·VB下几个非常有用的函数-.NET教程,VB.Net语言

最新文章
·VC#初学入门:第一个Windows程序
·ASP.NET 2.0-选用DataSet或DataReader
·用.net 处理xmlHttp发送异步请求
·asp.net创建文件夹的IO类的问题
·asp.net 2.0 中加密web.config 文件中的配置节
·关于ASP.NET调用JavaScript的实现
·如何实现ASP.NET网站个性化
·Acegi安全系统的配置-.NET教程,评论及其它
·Spring安全系统:Acegi Security Acegi简介-.NET教程,评论及其它
·Biztalk 开发之 架构和实例的验证-.NET教程,评论及其它


 
 


版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!

特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
  打印  刷新  关闭
返回首页 |关于我们 | 联系我们 | 付款方式 | 创业联盟 | 虚拟主机 | 资讯中心 | 友情链接 | 网站地图

版权所有 西部数码(www.west263.com)
CopyRight (c) 2002~2006 west263.com all right reserved.
公司地址:四川成都市万和路90号天象大厦4楼 邮编:610031
电话总机:028-86262244 86263048 86263408 86263960 86264018 86267838
售前咨询:总机转201 202 203 204 206 208
售后服务:总机转211 212 213 214
财务咨询:总机转224 223 传真:028-86264041 财务QQ:点击发送消息给对方635483282
售前咨询QQ:点击发送消息给对方2182518 点击发送消息给对方241975952 点击发送消息给对方275026793 点击发送消息给对方408235859
售后服务QQ:点击发送消息给对方17708515 点击发送消息给对方307742704 点击发送消息给对方287976517 点击发送消息给对方363783715
《中华人民共和国增值电信业务经营许可证》编号:川B2-20030065号