使用ICallbackEventHandler接口实现页面无刷新
2006-11-16 11:15
701 查看
此接口属于.net framework 2.0新特性。
MSDN原文如下:
下面是简化的脚本
MSDN原文如下:
<%@ Page Language="C#" %> <%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> public int cbCount = 0; // Define method that processes the callbacks on server. public void RaiseCallbackEvent(String eventArgument) { cbCount = Convert.ToInt32(eventArgument) + 1; } // Define method that returns callback result. public string GetCallbackResult() { return cbCount.ToString(); } protected void Page_Load(object sender, EventArgs e) { // Define a StringBuilder to hold messages to output. StringBuilder sb = new StringBuilder(); // Check if this is a postback. sb.Append("No page postbacks have occurred."); if (Page.IsPostBack) { sb.Append("A page postback has occurred."); } // Write out any messages. MyLabel.Text = sb.ToString(); // Get a ClientScriptManager reference from the Page class. ClientScriptManager cs = Page.ClientScript; // Define one of the callback script's context. // The callback script will be defined in a script block on the page. StringBuilder context1 = new StringBuilder(); context1.Append("function ReceiveServerData1(arg, context)"); context1.Append("{"); context1.Append("Message1.innerText = arg;"); context1.Append("value1 = arg;"); context1.Append("}"); // Define callback references. String cbReference1 = cs.GetCallbackEventReference(this, "arg", "ReceiveServerData1", context1.ToString()); String cbReference2 = cs.GetCallbackEventReference("'" + Page.UniqueID + "'", "arg", "ReceiveServerData2", "", "ProcessCallBackError", false); String callbackScript1 = "function CallTheServer1(arg, context) {" + cbReference1 + "; }"; String callbackScript2 = "function CallTheServer2(arg, context) {" + cbReference2 + "; }"; // Register script blocks will perform call to the server. cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer1", callbackScript1, true); cs.RegisterClientScriptBlock(this.GetType(), "CallTheServer2", callbackScript2, true); } </script> <script type="text/javascript"> var value1 = 0; var value2 = 0; function ReceiveServerData2(arg, context) { Message2.innerText = arg; value2 = arg; } function ProcessCallBackError(arg, context) { Message2.innerText = 'An error has occurred.'; } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server"> <title>ClientScriptManager Example</title> </head> <body> <form id="Form1" runat="server"> <div> Callback 1 result: <span id="Message1">0</span> <br /> Callback 2 result: <span id="Message2">0</span> <br /> <br /> <input type="button" value="ClientCallBack1" onclick="CallTheServer1(value1, alert('Increment value'))"/> <input type="button" value="ClientCallBack2" onclick="CallTheServer2(value2, alert('Increment value'))"/> <br /> <br /> <asp:Label id="MyLabel" runat="server"></asp:Label> </div> </form> </body> </html> |
<%@ Page Language="C#" AutoEventWireup="true" Debug="true" CodeFile="Index.aspx.cs" Inherits="Index" %> <!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>荒野实验室</title> < /head> <body> <form id="Form1" runat="server"> <div> <asp:TextBox ID="TestTxt" runat="server"></asp:TextBox> <br /> <br /> <input onclick="CallTheServer(TestTxt.value)" type="button" value="提交" /> <br /> <br /> </div> </form> </body> </html> CodeFile: using System; using System.Collections; using System.Configuration; using System.Data; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; public partial class Index : System.Web.UI.Page, ICallbackEventHandler { string returnValue; protected void Page_Load(object sender, EventArgs e) { string ReceiveScript = @" function ReceiveServerData(arg) { document.forms['Form1'].TestTxt.value = arg; }"; ClientScript.RegisterClientScriptBlock(this.GetType(), "ReceiveServerData", ReceiveScript, true); string CallbackScript = @" function CallTheServer(arg) { " + ClientScript.GetCallbackEventReference(this, "arg", "ReceiveServerData", null) + @"; } "; ClientScript.RegisterClientScriptBlock(this.GetType(), "CallTheServer", CallbackScript, true); } // 接收客户端消息 public void RaiseCallbackEvent(string eventArgument) { if (eventArgument != "") returnValue = eventArgument + 1; } // 返回客户端 public string GetCallbackResult() { return returnValue; } } |
相关文章推荐
- 使用ICallbackEventHandler接口实现的轻量级下拉框联动
- ASP.Net页面实现ICallbackEventHandler接口,不能工作的问题。
- ASP.NET无刷新客户端回调(通过实现ICallbackEventHandler接口)
- 使用ICallbackEventHandler接口实现的轻量级下拉框联动
- 使用ICallbackEventHandler 实现异步调用
- 使用ICallBackEventHandler接口,后台使用Response.End问题的解决
- 关于使用ICallbackEventHandler接口 ,传递中文时的错误
- 利用ICallbackEventHandle实现类似AJAX的无刷新页面
- 在自定义控件中实现ICallbackEventHandler接口不经过回发而实现客户端回掉
- 在自定义控件中实现ICallbackEventHandler接口不经过回发而实现客户端回掉
- 使用Ext.util.TaskRunner类实现,Ext的API中有示例,grid页面自动刷新
- 六步使用ICallbackEventHandler实现无刷新回调
- 使用XML实现页面的局部刷新
- 使用Dojo实现页面不刷新提交数据
- 六步使用ICallbackEventHandler实现无刷新回调
- 使用pushState实现微信“返回”按钮控制单页应用页面的无刷新跳转
- 利用ICallbackEventHandler接口实现无刷新的方法(3)--示例
- 使用Jquery的Ajax实现无刷新更新,修改,删除页面
- 使用ICallbackEventHandler实现无刷新回调
- 使用iframe实现提交表单不刷新页面