2008年6月4日 星期三

PageRequestManager

每一個使用 ASP.NET AJAX 的 .aspx 頁面都會放一個 ScriptManager 控制項。這控制項裡存放著控制 UpdatePanel 所需的 javascript 函式。不管是 ScriptManager 還是 UpdatePanel ,背後都還有一個老闆在監督著,這就是 PageRequestManager。

擔當非同步處理的 PageRequestManager,也是有所謂的生命週期。他共有5種:

Event 順序Event 說明
1. initializeRequestRaised before processing of the asynchronous request starts. You can use this event to cancel a postback.
2. beginRequestRaised before processing of an asynchronous postback starts and the postback is sent to the server. You can use this event to set request headers or to begin an animation that indicates that the page is processing.
3. pageLoadingRaised after the response from the server to an asynchronous postback is received but before any content on the page is updated. You can use this event to provide a custom transition effect for updated content.
4. pageLoadedRaised after all content on the page is refreshed, as a result of either a synchronous or an asynchronous postback. You can use this event to provide a custom transition effect for updated content.
5. endRequestRaised after an asynchronous postback is finished and control has been returned to the browser. You can use this event to provide a notification to users or to log errors.



攔PageRequestManager5種生命週期的方法:

<script type="text/javascript">
var prm=Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(initializeRequest);
prm.add_beginRequest(beginRequest);
prm.add_pageLoading(pageLoading);
prm.add_pageLoaded(pageLoaded);
prm.add_endRequest(endRequest);

function initializeRequest(sender, initializeRequestEventArgs)
{
alert('initialize function');
}

function beginRequest(sender, beginRequestEventArgs)
{
alert('beginRequest function');
}

function pageLoading(sender, pageLoadingEventArgs)
{
alert('pageLoading function');
}

function pageLoaded(sender, pageLoadedEventArgs)
{
alert('pageLoaded function');
}

function endRequest(sender, endRequestEventArgs)
{
alert('endRequest function');
}
</script>

[程式參考]

沒有留言:

張貼留言