<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

    谈起客户端无刷新远程调用,其好处无需多言。其实现方式除了早些年流行的iframe异步加载外,现在更多的是用基于JavaScript+xmlhttprequest对象技术,比如如日中天的Ajax。而在这里我们介绍的是ASP.NET本身对客户端远程调用的支持--客户端回调(念diao),当然了,它的本质也是XMLhttprequest。类似的帖子在网上已经被翻烂了,如果嫌这里说的不过瘾,可以自己动手看看高手们怎么说。

 

创建实现客户端回调的 ASP.NET 页必须执行以下操作:

·         实现 <?xml:namespace prefix = u1 />接口。可以向任何 ASP.NET 网页添加此接口声明。

·         实现 接口的方法。此方法将由回调来调用,执行一些逻辑处理。

·         实现 接口的方法。此方法将向clientCallBack 函数返回一个字符串

注意:实现接口的方法必须为public,否则不能被远程调用。

 

此外,该页还必须包含执行以下操作的三个客户端脚本函数:
·         一个函数调用帮助器方法,该方法执行对服务器的实际请求。在此函数中,可以首先执行自定义逻辑以准备事件参数,然后可以将一个字符串作为参数发送到服务器端回调事件处理程序。
·         另一个函数由处理回调事件的服务器代码的结果调用并接收该结果,同时接受表示该结果的字符串。该函数就是上面所说的 clientCallBack 函数。
·         第三个函数是执行对服务器的实际请求的 Helper 函数,当在服务器代码中使用 方法生成对此函数的引用时,由 ASP.NET 自动生成该函数。
我这里做了一个最简单的例子,只有一个WebForm,界面如下:
 <?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />
实现,在文本框里输入文本,点击“服务器回显”按钮后,回显结果显示文本框的输入。
页面代码如下:
<% @ 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>Untitled Page</title>

      <script type="text/javascript">

    function LookUpStock()

    {

        CallServer(document.form1.Text1.value, "");

    }

    function ReceiveServerData(rValue)

    {

        Results.innerText = rValue;

    }

  </script>

</ head >

< body >

    <form id="form1" runat="server">

    <div>

        <input id="Text1" type="text" />

      <button onclick="LookUpStock()"> 服务器回显</button>

      <br />

      服务器回显结果: <span id="Results"></span>

      <br />

    </div>

    </form>

</ body >

</ html >

 

后台代码如下:
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,System.Web.UI.ICallbackEventHandler

{

    protected string returnValue;

    protected void Page_Load(object sender, EventArgs e)

{

// 以下代码没有什么好解释的,除了ReceiveServerData函数的名称,几乎不允许你有什么变化。有人当然喜欢在页面中写客户端脚本,根据个人喜好,不要抠字眼。
        String cbReference =Page.ClientScript.GetCallbackEventReference(this,"arg", "ReceiveServerData", "context");

        String callbackScript = "function CallServer(arg, context)" +

            "{ " + cbReference + "} ;";

        Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"CallServer", callbackScript, true);

    }

    #region ICallbackEventHandler Members

    public string GetCallbackResult()

    {

        return returnValue;

}

    public void RaiseCallbackEvent(string eventArgument)

    {

        returnValue = eventArgument;

    }

    #endregion

}

 

这可真是全部代码啊,呵呵。如果这样都没法实现上述功能,那我也没辙了。不过比较遗憾的是,传入传出参数只能是字符串,因此如果数据比较复杂,比如对象,需要额外步骤处理。

 

其实这个例子很简单,但是我花了两个小时调试,最后发现竟然死在vs2005工具上,大意啦~~~ 一般实现接口的时候,可以利用VS2005强大的智能感应,可是这次似乎有点问题,我用的是Pro中文版,加了SP1,当鼠标放在接口上时,显示如下:
点击后得到的自动代码如下:
    #region ICallbackEventHandler 成员

    string ICallbackEventHandler.GetCallbackResult()

    {

        throw new Exception("The method or operation is not implemented.");

    }

    void ICallbackEventHandler.RaiseCallbackEvent(string eventArgument)

    {

        throw new Exception("The method or operation is not implemented.");

    }

#endregion

稍加修改后,怎么运行都无法实现客户端回调,后来才发现这种显示实现方式在这里不适应。所以就手工写了上述实现函数。但是在VS2005英文版中却有两个分菜单,用显示实现就可以实现,不知道微软这样做是何用意。看来还是不敢将全部托付给工具啊。