现在的位置: 首页 > 综合 > 正文

ICallbackEventHandler

2013年09月15日 ⁄ 综合 ⁄ 共 4154字 ⁄ 字号 评论关闭
ICallbackEventHandler 
ICallbackEventHandler可以说是ajax的关键技术,现在可以使用vs2.0的接口,来实现ajax的效果。
refer to:http://www.cnblogs.com/soso/archive/2006/06/08/420687.html
六步使用ICallbackEventHandler实现无刷新回调
    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)%>;
完整的测试代码:
aspx:
==============
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ICallbackEventHandler demo</title>
    <script type="text/javascript">
        function CallServer()
        {
            var product="hello";
            <%=ClientScript.GetCallbackEventReference(this,"product","ReceiveServerData",null) %>;
        }
        function ReceiveServerData(rValue)
        {
            alert(rValue);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Button1" type="button" value="CallServer" onclick="CallServer()" />
    </div>
    </form>
</body>
</html>
cs:
============
using System;
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page , ICallbackEventHandler
{
    private string CallBackValue = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
    }
   #region ICallbackEventHandler 成员
    string ICallbackEventHandler.GetCallbackResult()
    {
        //throw new Exception("The method or operation is not implemented.");
        return CallBackValue + ", i am server";
    }
    void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)
    {
        //throw new Exception("The method or operation is not implemented.");
        this.CallBackValue = eventArgument;
    }

抱歉!评论已关闭.