当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 浅析ASP.NET 2.0 Client Callback

ASP.NET
FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
.NET 常用功能和代码小结
在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出
asp.net IList查询数据后格式化数据再绑定控件
asp.net sql存储过程
asp.net 简单实现禁用或启用页面中的某一类型的控件
asp.net(c#)获取内容第一张图片地址的函数
The remote procedure call failed and did not execute的解决办法
ASP.NET 在线文件管理
asp.net 读取并修改config文件实现代码
ASP.NET Cookie 操作实现
asp.net Silverlight中的模式窗体
Silverlight中动态获取Web Service地址
asp.net Silverlight应用程序中获取载体aspx页面参数
asp.net 水晶报表隔行换色实现方法
asp.net 获取Gridview隐藏列的值
手动把asp.net的类生成dll文件的方法
asp.net 使用ObjectDataSource控件在ASP.NET中实现Ajax真分页
动态指定任意类型的ObjectDataSource对象的查询参数
asp.net Md5的用法小结

浅析ASP.NET 2.0 Client Callback


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-23   浏览: 161 ::
收藏到网摘: n/a

GridView控件正是使用了这个技术来实现无刷新的分页及排序功能,那么它是如何实现的哪,我们又如何在自定义控件中添加该功能哪?本文将会简单地阐述如何使用ASP.NET 2.0 Callback来实现Ajax.

1、ICallbackEventHandler 接口

ASP.NET Server端控件可以通过实现ICallbackEventHandler 接口来接受客户端Callback事件。
ICallbackEventHandler 接口声明:

RaiseCallbackEvent负责处理客户端回调事件,其中方法参数eventArgument是客户端脚本在触发Callback时提供的,该方法将会依赖于eventArgument参数来进行处理。
GetCallbackResult则负责将处理结果作为String返回给客户端脚本。当Callback完成后客户端脚本将会根据得到的处理结果,进行页面局部更新。

2、CallbackEventReference

那么怎么样才能注册一段客户端脚本来触发Callback,还需要做些什么哪?

ClientScriptManager类用于管理Web页面中的客户端脚本,提供了一系列的方法来注册脚本,并且还可以获得指定客户端脚本函数的引用。通过ClientScriptManager类的GetCallbackEventReference方法我们可以获取一个对客户端函数的引用。当该函数在客户端被调用时,将启动一次客户端回调。

GetCallbackEventReference方法声明:

以下为引用的内容:

public string GetCallbackEventReference (
Control control,
string argument,
string clientCallback,
string context,
string clientErrorCallback,
bool useAsync
)

第一个参数指的是实现ICallbackEventHandler接口的服务器端控件;
第二个参数将被传递给在服务器端执行的RaiseCallbackEvent方法,它可以是一个JavaScript函数调用表达式;
第三个参数是一个JavaScript函数名,在Callback完成后,该函数将被调用,同时服务器端函数GetCallbackResult的执行结果也将作为这一个函数的参数;
第四个参数是当前执行的Callback的上下文,这个参数也可以是一个JavaScript函数调用表达式; 
第五个参数是一个JavaScript函数名,在Callback执行的过程中如果有错误产生,该函数将被调用。
第六个参数是一个Bool值来确定当前Callback应该被同步执行还是异步执行。

获取这个Callback客户端函数的引用之后,我们可以注册一个新的客户端函数来调用它。然后再客户端就可以通过新注册的函数来进行Callback了。

3.示例

我们通过一个简单的例子来剖析ASP.NET 2.0 Callback的整个执行过程:

以下为引用的内容:

1public class MyControl : WebControl, ICallbackEventHandler
2    {
3        private const string Script1 = "function onCallbackComplete(result){ \n" +
4                "   var element = document.getElementById('%ID%'); \n" +
5                "   if(element != null) \n" +
6                "       element.innerHTML = result;} \n";
7
8        private const string Script2 = "function onCallbackError(){ \n" +
9                "   var element = document.getElementById('%ID%'); \n" +
10                "   if(element != null) \n" +
11                "       element.innerHTML = 'error';} \n";
12
13        public string GetCallbackResult()
14        {
15            return "Callback result";
16        }
17
18        public void RaiseCallbackEvent(string eventArgument)
19        {
20        }
21
22        public override void RenderBeginTag(HtmlTextWriter writer)
23        {
24            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "DoClientCallBack()");
25            base.RenderBeginTag(writer);
26            writer.Write("My Callback control");
27        }
28
29        protected override void OnPreRender(EventArgs e)
30        {
31            //Define callback references.
32            string callbackRef = this.Page.ClientScript.GetCallbackEventReference(
33                this, "", "onCallbackComplete", null, "onCallbackError", true);
34
35            // Register script blocks will perform call to the server.
36            this.Page.ClientScript.RegisterClientScriptBlock(
37                this.GetType(), "DoClientCallBack",
38                "function DoClientCallBack() { " + callbackRef + "} \n", true
39            );
40
41            // Register other scripts
42            this.Page.ClientScript.RegisterClientScriptBlock(
43                this.GetType(), "onCallbackComplete",
44                Script1.Replace("%ID%", this.ClientID), true);
45            this.Page.ClientScript.RegisterClientScriptBlock(
46                this.GetType(), "onCallbackError",
47                Script2.Replace("%ID%", this.ClientID), true);
48
49            base.OnPreRender(e);
50        }

其中WebForm_DoCallback和WebForm_CallbackComplete是微软JavaScript库中的方法。

ASP.NET 2.0 Callback提供了一种简单的方法来使得ASP.NET Server段控件可以支持AJAX,其本身可以看作是一种轻量级的Postback。

全文完。

[更新 2008.05.27]

关于构建XMLHttpRequest时,设置useAsync参数为True和False的区别,我摘录了MSDN的解释:

bAsync Optional.

Variant that specifies true for asynchronous operation (the call returns immediately), or false for synchronous operation. If true, assign a callback handler to the onreadystatechange property to determine when the call has completed. If not specified, the default is true.

When bAsync is set to false, send operations are synchronous, and Windows Internet Explorer does not accept input or produce output while send operations are in progress. Therefore, this setting should not be used in situations where it is possible for a user to be waiting on the send operation to complete.

注意黑体字部分,另外还有一点区别就是,当这次请求是同步的时候,是没有办法Cancel的。也就是说只有在bAsync为True的时候,XmlHttpRequest对象的Abort方法才会生效。