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

2016-02-19 15:28 8 1 收藏

下面图老师小编跟大家分享六步使用ICallbackEventHandler实现无刷新回调,一起来学习下过程究竟如何进行吧!喜欢就赶紧收藏起来哦~

【 tulaoshi.com - Web开发 】

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

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

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

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

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

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

  
   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)
  

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/webkaifa/)

来源:https://www.tulaoshi.com/n/20160219/1609703.html

延伸阅读
六步做好老人护眼工作 人上了年纪之后,身体会出现各种状况,其中最容易出问题的要数眼睛,常见的如青光眼、白内障等疾病会随时找上老年人。那么,老年人在日常生活中该如何避免眼疾伤害? 一、远眺运目 老年人一旦感到眼睛酸疼时,不妨马上去室外看看远处的山峰、楼顶、塔尖等,这样可调节眼肌和晶状体,减轻眼睛的疲劳...
标签: 改造 水管
水电改造 1.仔细查看是否漏做开工交底时规定的水电线路,若漏掉要及时补上,一拖就会忘掉。 2.用万能表测强、弱电是否通畅,若不通畅,中间肯定有断头,要及时换线。 3.将冷热水连接起来并逐个放开堵头,看所有水管是否通水,千万不要等到瓷砖、马桶、浴缸、洗脸盆等都装好了才发现水管不通,那就麻烦了。 4.水管焊接好以后一定要做水压...
声明函数指针并实现回调 作者:Danny Kalev 编译:赵湘宁 原文出处:Declaring Function Pointers and Implementing Callbacks     程序员常常需要实现回调。本文将讨论函数指针的基本原则并说明如何使用函数指针实现回调。注意这里针对的是普通的...
六步轻松勾勒潮流猫眼妆 性感猫眼妆无论何时何地,都是潮流的另一个别名。三角形眼线早已攻占欧美各大潮流区域,韩国女星也纷纷效仿,你还在等什么呢?从眼头开始一气呵成的眼线,彰显出性感与个性。 STEP 1 眼周加强打底,眼妆更持久 面部完成打底后,在眼部周围的眼皮再增加一层打底,遮盖眼周的暗沉,加强明亮感,同时...
标签: 孕前
计划怀孕 请分六步走! 提前停止避孕措施 在你们开始准备怀宝宝的几个月之前,就要开始停止你的避孕措施。因为在你停止使用避孕措施后,身体需要经过几个周期才能够回复到正常的排卵从而进行受孕,而且这样你才能通过你的生理周期估测你的排卵时间。 找出你的排卵期 无论你跟丈夫进行性生活的频率...

经验教程

722

收藏

34
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部