代码改变世界

ICallbackEventHandler实现回调

2011-07-13 17:55  雪中风筝  阅读(319)  评论(0编辑  收藏  举报

在上一篇文章中介绍了什么叫回调,这篇使用ASP.Net中的ICallbackEventHandler实现回调,该网页模拟一个数据库查找,以确定一系列产品(监视器、键盘等)的供货数量或库存数量。为了简化此代码示例,数据库由包含少量物品的词典列表来表示。对于表中的每件物品,键就是物品名称(如监视器),值就是物品的库存数。但是在成品应用程序中,将使用数据库。

 1 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CallBack.aspx.cs" Inherits="CallBackDemo.CallBack" %>
 2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 3 
 4 <html xmlns="http://www.w3.org/1999/xhtml">
 5 <head runat="server">
 6     <title>Client Callback Demo</title>
 7     <script type ="text/ecmascript">
 8         function LookupStock() 
 9         {
10             var lb = document.getElementById("ListBox1");
11             var product = lb.options[lb.selectedIndex].text;
12             CallServer(product, "");
13         }
14 
15         function ReceviveServerData(rValue) {
16             document.getElementById("ResultSpan").innerHTML = rValue;
17         }
18     </script>
19 </head>
20 <body>
21     <form id="form1" runat="server">
22     <div>
23         <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
24         <br/>
25         <br/>
26         <button type="button" onclick="LookupStock()" >LookupButton</button>
27         <br />
28         <br />
29         Items in stock: <span id = "ResultSpan" runat="server"></span>
30         <br />
31     </div>
32     </form>
33 </body>
34 </html>

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 using System.Web.UI;
 6 using System.Web.UI.WebControls;
 7 
 8 namespace CallBackDemo
 9 {
10     public partial class CallBack : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler
11     {
12         protected string returnValue = string.Empty;
13         protected System.Collections.Specialized.ListDictionary catalog;
14 
15         protected void Page_Load(object sender, EventArgs e)
16         {
17             //获取客户端回调函数的引用,GetCallbackEventReference方法的参数请参考msdn
18 
19             string cbReference = Page.ClientScript.GetCallbackEventReference
20                 (this"arg""ReceviveServerData""context");
21 
22             //注册客户端函数
23             string callbackSript =
24                 "function CallServer(arg,context) {" + cbReference + "};";
25             Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackSript, true);
26 
27 
28             catalog = new System.Collections.Specialized.ListDictionary();
29             catalog.Add("monitor"12);
30             catalog.Add("loptop"10);
31             catalog.Add("keybord"23);
32             catalog.Add("mouse"17);
33 
34             ListBox1.DataSource = catalog;
35             ListBox1.DataTextField = "key";
36             ListBox1.DataBind();
37         }
38 
39        
40         //接受客户端发给服务器端的参数
41         public void RaiseCallbackEvent(string eventArgument)
42         {
43             if (catalog[eventArgument] == null)
44             {
45                 returnValue = "-1";
46             }
47             else
48             {
49                 returnValue = catalog[eventArgument].ToString();
50             }
51         }
52 
53         //服务器端返回给客户端参数的值
54         public string GetCallbackResult()
55         {
56             return returnValue;
57         }
58     }
59 }

当运行此页时,ListBox 控件被绑定到哈希表,这样,ListBox控件便可以显示产品列表。此页还包含一个 button 元素(非 Button Web 服务器控件),其 onclick 事件被绑定到一个名为 LookUpStock 的客户端函数。当用户单击按钮时,该按钮便会执行 LookUpStock 函数,此函数从列表框中获取当前所选内容,然后通过调用 CallServer 函数来执行客户端回调。

代码隐藏页通过 RegisterClientScriptBlock 方法向该页添加客户端脚本。添加到该页的脚本包括一个称为 CallServer 的函数,此函数用于获取将从 GetCallbackEventReference 方法回发到服务器的方法的名称。

客户端回调会调用 RaiseCallbackEvent 方法,以确定传递给它的产品的可用库存。GetCallbackResult方法将返回该值。请注意,在客户端脚本与服务器代码之间发送的参数只能是字符串。若要传入或接收多个值,可以分别在输入字符串或返回字符串中将这些值串连起来 

代码来自MSDN,原文地址:http://msdn.microsoft.com/zh-cn/library/ms178210(v=VS.90).aspx