大家好,欢迎来到我这里

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

如果使用过UpdatePanel,可能已经注意到,当一个页面有多个UpdatePanel的时候,异步页面请求同时只能有一个请求(callback),而且后发起的请求会取消先发的请求。怎么改变这个次序呢,比如一个异步请求已经在进行,怎么阻止其它请求呢?这就要用到PageRequestManager。(让多个请求并发进行,以后会介绍)

通过PageRequestMangaer还能提供更多的feature:提供取消callback的选项,自定义错误处理消息,访问底层进行异步请求的request/response对象。

总的来看,PageRequestManager是运行在浏览器端的协调者,它和服务器端的UpdatePanel一起,把局部页面更新的复杂性抽象化,提供给开发者方面的使用接口。

PageRequestManager提供了以下事件,让开发者能加入自己的Javascript脚本来改变缺省的行为:

1. initializeRequest

Raised before the request is initialized for an asynchronous postback. Event data is passed to handlers as an InitializeRequestEventArgs object. The object makes available the element that caused the postback and the underlying request object.

2. beginRequest

Raised just before the asynchronous postback is sent to the server. Event data is passed to handlers as a BeginRequestEventArgs object. The object makes available the element that caused the postback and the underlying request object.

3. pageLoading

Raised after the response to the last asynchronous postback has been received but before any updates to the page have been made. Event data is passed to handlers as a PageLoadingEventArgs object. The object makes available information about what panels will be deleted and updated as a result of the last asynchronous postback

4. pageLoaded

Raised after page regions are updated after the last asynchronous postback. Event data is passed to handlers as a PageLoadedEventArgs object. The object makes available information about what panels were created and updated. For synchronous postbacks, panels are only created, but for asynchronous postbacks panels can be both created and updated.

5. endRequest

Raised when request processing is finished. Event data is passed to handlers as an EndRequestEventArgs object. The object makes available information about errors that have occurred and whether the error was handled. It also makes available the response object.

 

 

我们来分析一个使用PageRequestManager的例子:

Run the example ( view source)

这个例子中,使用了PageRequestManager来定制Update状态显示,也提供了取消请求的方法,这主要是从下面的代码中展示出来:

<script type="text/javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
var postBackElement;
function InitializeRequest(sender, args) {
if (prm.get_isInAsyncPostBack())
{
   args.set_cancel(true);
}
postBackElement = args.get_postBackElement();
if (postBackElement.id == 'ButtonTrigger')
{
   $get('UpdateProgress1').style.display = "block";
}
}
function EndRequest (sender, args) {
if (postBackElement.id == 'ButtonTrigger')
{
   $get('UpdateProgress1').style.display = "none";
}
}
function AbortPostBack() {
if (prm.get_isInAsyncPostBack()) {
   prm.abortPostBack();
}
}
</script>

在例子中,通过给PageRequestManager的initializeRequest事件和endRequest事件加入自定义的javascript代码,实现了定制的行为。

对于PageRequestManager,使用get_isInAsyncPostBack方法或isInAsyncPostBack属性能得到当前异步请求状态;使用abortPostBack可以取消当前的请求;使用add_initializeRequest和add_endRequest分别将用户编写的javascript处理函数注册到initializeRequest事件和endRequest事件中。

另外,在函数InitializeRequest中,args.set_cancel(true)用来让先发的请求被执行,改变缺省的后发具优的行为。


得到PageRequestManager的所有方法和事件列表,请参见:
Sys.WebForms.PageRequestManager Class

posted on 2007-04-19 08:44  zwgood  阅读(329)  评论(0编辑  收藏  举报