ASP.NET 客户端回调实现 (C#) 示例 (VS2005)MSDN 错误处理
2008-11-25 17:11
1016 查看
最近研究了一下ASP.NET 客户端回调,在MSDN上找到了这个示例:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClientCallback.aspx.cs" Inherits="ClientCallback" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <script type="text/javascript"> function LookUpStock() { var lb = document.forms[0].ListBox1; var product = lb.options[lb.selectedIndex].text CallServer(product, ""); } function ReceiveServerData(rValue) { Results.innerText = rValue; } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:ListBox ID="ListBox1" Runat="server"></asp:ListBox> <br /> <br /> <button onclick="LookUpStock()">Look Up Stock</button> <br /> <br /> Items in stock: <span ID="Results"></span> <br /> </div> </form> </body> </html> // ClientCallback.aspx.cs code-behind page using System; using System.Data; using System.Configuration; using System.Collections; 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 ClientCallback : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler { protected System.Collections.Specialized.ListDictionary catalog; protected void Page_Load(object sender, EventArgs e) { String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context"); String callbackScript; callbackScript = "function CallServer(arg, context)" + "{ " + cbReference + "} ;"; Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true); catalog = new System.Collections.Specialized.ListDictionary(); catalog.Add("monitor", 12); catalog.Add("laptop", 10); catalog.Add("keyboard", 23); catalog.Add("mouse", 17); ListBox1.DataSource = catalog; ListBox1.DataTextField = "key"; ListBox1.DataBind(); } public String RaiseCallbackEvent(String eventArgument) { String returnValue; if (catalog[eventArgument] == null) { returnValue = "-1"; } else { returnValue = catalog[eventArgument].ToString(); } return returnValue; } }
但执行有错误,System.Web.UI.ICallbackEventHandler 此接口方法实现的不正确。下面咱们看一下此接口的声明:
using System;
namespace System.Web.UI { // 摘要: // 用于指示控件可以作为服务器的回调事件的目标。 public interface ICallbackEventHandler { // 摘要: // 返回以控件为目标的回调事件的结果。 // // 返回结果: // 回调的结果。 string GetCallbackResult(); // // 摘要: // 处理以控件为目标的回调事件。 // // 参数: // eventArgument: // 一个字符串,表示要传递到事件处理程序的事件参数。 void RaiseCallbackEvent(string eventArgument); } }
看明白了吧,MSDN示例 实现System.Web.UI.ICallbackEventHandler 这个接口有问题
我更改了一下 RaiseCallbackEvent(string eventArgument)此方法返回值应该是无类型的,还有必须实现此方法:string GetCallbackResult() 。更改后此示例就通过了,下面是修改后的源码:
using System; using System.Data; using System.Configuration; using System.Collections; 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 ClientCallback : System.Web.UI.Page,System.Web.UI.ICallbackEventHandler { protected System.Collections.Specialized.ListDictionary catalog; public string returnValue = string.Empty; protected void Page_Load(object sender, EventArgs e) { String cbReference = Page.ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", "context"); String callbackScript; callbackScript = "function CallServer(arg, context)" + "{ " + cbReference + "} ;"; Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "CallServer", callbackScript, true);
catalog = new System.Collections.Specialized.ListDictionary(); catalog.Add("monitor", 12); catalog.Add("laptop", 10); catalog.Add("keyboard", 23); catalog.Add("mouse", 17);
ListBox1.DataSource = catalog; ListBox1.DataTextField = "key"; ListBox1.DataBind(); }
public void RaiseCallbackEvent(String eventArgument) { if (catalog[eventArgument] == null) { returnValue = "-1"; } else { returnValue = catalog[eventArgument].ToString(); } //return returnValue; }
// Define method that returns callback result. public string GetCallbackResult() { return returnValue; } } 不知大家看明白了没有
相关文章推荐
- ASP.NET 客户端回调实现 (C#) 示例
- 在 ASP.NET 网页中不经过回发而实现客户端回调 [转MSDN]
- asp.net 客户端回调功能的实现机制探讨(响应部分及可能的优化)
- ASP.NET 2.0客户端回调功能实现
- 【C#】对异步请求处理程序IHttpAsyncHandler的理解和分享一个易用性封装 【手记】走近科学之为什么明明实现了IEnumerable<T>的类型却不能调用LINQ扩展方法 【手记】手机网页弹出层后屏蔽底层的滑动响应 【手记】ASP.NET提示“未能创建类型”处理 【Web】一个非常简单的移动web消息框 【手记】解决EXCEL跑SQL遇“查询无法运行或数据库表无法打开...”
- 每日总结(二)C#判断输入内容为数字、Sharepoint回收应用程序池错误、Asp.Net 会发或回调参数无效、DropDownList动态绑定且设置默认选择项
- ASP.NET 2.0客户端回调的实现分析(2)
- asp.net错误页面处理示例分享
- asp.net(C#)实现功能强大的时间日期处理类完整实例
- 最近写 ASP.NET 中出现的错误 & 实现DataTable和DataSet类型的客户端/服务器端自动转换
- asp.net(c#)编程实现将彩色图片变灰阶图片的方法示例
- 学习笔记(1)在 ASP.NET 网页中不经过回发而实现客户端回调
- Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
- Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)
- 在 ASP.NET 网页中不经过回发而以编程方式实现客户端回调
- c#(asp.net) 多线程示例,用于同时处理多个任务
- ASP.NET 2.0客户端回调的实现分析
- [修炼内功]ASP.NET 2.0客户端回调实现无刷新页面
- Asp.net 2.0 C#实现压缩/解压功能 (示例代码下载)