asp.net实现无刷新窗体的关键接口是ICallbackEventHandler,在MSDN对无刷新窗体的c#示例代码中存在了多个明显的错误。
原MSDN示例地址:ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_aspnetcon/html/2c688b1a-93a4-4dad-b82b-63974bdbb13e.htm
原代码如下:
第一个低级的BUG是32行的span 命名的ID关键字大写,这个会在编译时报错,必须更改为小写才可通过。
第二个BUG我估计是Studio 2005测试版和正式发布版不同造成的,因为这段c#代码根本不能通过编译,最起码会报两个错误,其关键是RaiseCallbackEvent这个必须实现的接口函数在正式发布的时候已经被拆分成两个函数,一个是public void RaiseCallbackEvent(string arg), 另一个是public string GetCallbackResult(),其实微软的想法很容易理解,首先通过RaiseCallbackEvent(string arg)执行客户端需要执行的服务器功能,然后通过GetCallbackResult()来向客户端反馈需要客户端处理的信息。
在MSDN的ICallbackEventHandler接口成员说明部分,已经提及了经过拆分后的两个函数,地址如下:
ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref13/html/T_System_Web_UI_ICallbackEventHandler_Members.htm
但随后的举例中并没有更改,这显然是一个文档的BUG。
我虽然在微软做测试工作,但这毕竟不是我职责范畴,希望大家能注意这个文档BUG,在研究或使用无刷新页面时,不要浪费不必要的时间。
有时间我会上PS中检查这个BUG是否提交过。
原MSDN示例地址:ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_aspnetcon/html/2c688b1a-93a4-4dad-b82b-63974bdbb13e.htm
原代码如下:
1 <%@ Page Language="C#" AutoEventWireup="true"
2 CodeFile="ClientCallback.aspx.cs" Inherits="ClientCallback" %>
3
4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
5 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
6
7 <html xmlns="http://www.w3.org/1999/xhtml" >
8 <head runat="server">
9 <script type="text/javascript">
10 function LookUpStock()
11 {
12 var lb = document.forms[0].ListBox1;
13 var product = lb.options[lb.selectedIndex].text
14 CallServer(product, "");
15 }
16
17 function ReceiveServerData(rValue)
18 {
19 Results.innerText = rValue;
20 }
21 </script>
22 </head>
23 <body>
24 <form id="form1" runat="server">
25 <div>
26 <asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox>
27 <br />
28 <br />
29 <button onclick="LookUpStock()">Look Up Stock</button>
30 <br />
31 <br />
32 Items in stock: <span ID="Results"></span>
33 <br />
34 </div>
35 </form>
36 </body>
37 </html>
38
39 // ClientCallback.aspx.cs code-behind page
40 using System;
41 using System.Data;
42 using System.Configuration;
43 using System.Collections;
44 using System.Web;
45 using System.Web.Security;
46 using System.Web.UI;
47 using System.Web.UI.WebControls;
48 using System.Web.UI.WebControls.WebParts;
49 using System.Web.UI.HtmlControls;
50
51 public partial class ClientCallback : System.Web.UI.Page,
52 System.Web.UI.ICallbackEventHandler
53 {
54 protected System.Collections.Specialized.ListDictionary catalog;
55 protected void Page_Load(object sender, EventArgs e)
56 {
57 String cbReference =
58 Page.ClientScript.GetCallbackEventReference(this,
59 "arg", "ReceiveServerData", "context");
60 String callbackScript;
61 callbackScript = "function CallServer(arg, context)" +
62 "{ " + cbReference + "} ;";
63 Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
64 "CallServer", callbackScript, true);
65
66 catalog = new System.Collections.Specialized.ListDictionary();
67 catalog.Add("monitor", 12);
68 catalog.Add("laptop", 10);
69 catalog.Add("keyboard", 23);
70 catalog.Add("mouse", 17);
71
72 ListBox1.DataSource = catalog;
73 ListBox1.DataTextField = "key";
74 ListBox1.DataBind();
75 }
76
77 public String RaiseCallbackEvent(String eventArgument)
78 {
79 String returnValue;
80 if (catalog[eventArgument] == null)
81 {
82 returnValue = "-1";
83 }
84 else
85 {
86 returnValue = catalog[eventArgument].ToString();
87 }
88 return returnValue;
89 }
90 }
91
2 CodeFile="ClientCallback.aspx.cs" Inherits="ClientCallback" %>
3
4 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
5 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
6
7 <html xmlns="http://www.w3.org/1999/xhtml" >
8 <head runat="server">
9 <script type="text/javascript">
10 function LookUpStock()
11 {
12 var lb = document.forms[0].ListBox1;
13 var product = lb.options[lb.selectedIndex].text
14 CallServer(product, "");
15 }
16
17 function ReceiveServerData(rValue)
18 {
19 Results.innerText = rValue;
20 }
21 </script>
22 </head>
23 <body>
24 <form id="form1" runat="server">
25 <div>
26 <asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox>
27 <br />
28 <br />
29 <button onclick="LookUpStock()">Look Up Stock</button>
30 <br />
31 <br />
32 Items in stock: <span ID="Results"></span>
33 <br />
34 </div>
35 </form>
36 </body>
37 </html>
38
39 // ClientCallback.aspx.cs code-behind page
40 using System;
41 using System.Data;
42 using System.Configuration;
43 using System.Collections;
44 using System.Web;
45 using System.Web.Security;
46 using System.Web.UI;
47 using System.Web.UI.WebControls;
48 using System.Web.UI.WebControls.WebParts;
49 using System.Web.UI.HtmlControls;
50
51 public partial class ClientCallback : System.Web.UI.Page,
52 System.Web.UI.ICallbackEventHandler
53 {
54 protected System.Collections.Specialized.ListDictionary catalog;
55 protected void Page_Load(object sender, EventArgs e)
56 {
57 String cbReference =
58 Page.ClientScript.GetCallbackEventReference(this,
59 "arg", "ReceiveServerData", "context");
60 String callbackScript;
61 callbackScript = "function CallServer(arg, context)" +
62 "{ " + cbReference + "} ;";
63 Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
64 "CallServer", callbackScript, true);
65
66 catalog = new System.Collections.Specialized.ListDictionary();
67 catalog.Add("monitor", 12);
68 catalog.Add("laptop", 10);
69 catalog.Add("keyboard", 23);
70 catalog.Add("mouse", 17);
71
72 ListBox1.DataSource = catalog;
73 ListBox1.DataTextField = "key";
74 ListBox1.DataBind();
75 }
76
77 public String RaiseCallbackEvent(String eventArgument)
78 {
79 String returnValue;
80 if (catalog[eventArgument] == null)
81 {
82 returnValue = "-1";
83 }
84 else
85 {
86 returnValue = catalog[eventArgument].ToString();
87 }
88 return returnValue;
89 }
90 }
91
第一个低级的BUG是32行的span 命名的ID关键字大写,这个会在编译时报错,必须更改为小写才可通过。
第二个BUG我估计是Studio 2005测试版和正式发布版不同造成的,因为这段c#代码根本不能通过编译,最起码会报两个错误,其关键是RaiseCallbackEvent这个必须实现的接口函数在正式发布的时候已经被拆分成两个函数,一个是public void RaiseCallbackEvent(string arg), 另一个是public string GetCallbackResult(),其实微软的想法很容易理解,首先通过RaiseCallbackEvent(string arg)执行客户端需要执行的服务器功能,然后通过GetCallbackResult()来向客户端反馈需要客户端处理的信息。
在MSDN的ICallbackEventHandler接口成员说明部分,已经提及了经过拆分后的两个函数,地址如下:
ms-help://MS.MSDNQTR.v80.en/MS.MSDN.v80/MS.NETDEVFX.v20.en/cpref13/html/T_System_Web_UI_ICallbackEventHandler_Members.htm
但随后的举例中并没有更改,这显然是一个文档的BUG。
我虽然在微软做测试工作,但这毕竟不是我职责范畴,希望大家能注意这个文档BUG,在研究或使用无刷新页面时,不要浪费不必要的时间。
有时间我会上PS中检查这个BUG是否提交过。