Client Page Life-cycle Events
During ordinary page processing in the browser, the window.onload DOM event is raised when the page first loads. Similarly, the window.onunload DOM event is raised when the page is refreshed or when the user moves away from the page.
However, these events are not raised during asynchronous postbacks. To help you manage these types of events for asynchronous postbacks, the PageRequestManager class exposes a set of events. These resemble window.load and other DOM events, but they also occur during asynchronous postbacks. For each asynchronous postback, all page events in the PageRequestManager class are raised and any attached event handlers are called.
noteFor synchronous postbacks, only the pageLoaded event is raised.
You can write client script to handle events raised by the PageRequestManager class. Different event argument objects are passed to handlers for different events. The following table summarizes the PageRequestManager class events and the corresponding event argument classes. The order of the events in the table is the order of the events for a single asynchronous postback without errors.
- 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.
- 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.
- pageLoading
- Raised after the response to the most recent 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 most recent asynchronous postback.
- pageLoaded
- Raised after page regions are updated after the most recent postback. Event data is passed to handlers as a PageLoadedEventArgs object. The object makes available information about what panels were created or updated. For synchronous postbacks, panels can only be created, but for asynchronous postbacks, panels can be both created and updated.
- 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.
If you use the RegisterDataItem method of the ScriptManager control to send extra data during an asynchronous postback, you can access that data from the PageLoadingEventArgs, PageLoadedEventArgs, and EndRequestEventArgs objects.
The sequence of events varies with different scenarios. The order in the previous table is for a single, successful asynchronous postback. Other scenarios include the following:
- Multiple postbacks where the most recent postback takes precedence, which is the default behavior. Only events for the most recent asynchronous postback are raised.
- Multiple postbacks where one postback is given precedence, which cancels all subsequent postbacks until it is finished. Only the initializeRequest event is raised for canceled postbacks.
- An asynchronous postback that is stopped. Depending on when the postback is stopped, some events might not be raised.
- An initial request (HTTP GET) of a page, or a page refresh. When a page is first loaded or when it is refreshed in the browser, only the pageLoaded event is raised.
Check what id is requesting?
1 <script type=“text/javascript“ language=“javascript“>
2
3 /*
4 Sys.WebForms.PageRequestManager.instance.add_beginRequest(beginRequestHandler)
5 Sys.WebForms.PageRequestManager.instance.remove_beginRequest(beginRequestHandler)
6
7 Sys.WebForms.PageRequestManager.instance.add_endRequest(endRequestHandler)
8 Sys.WebForms.PageRequestManager.instance.remove_endRequest(endRequestHandler)
9
10 Sys.WebForms.PageRequestManager.instance.add_initializeRequest(initializeRequestHandler)
11 Sys.WebForms.PageRequestManager.instance.remove_initializeRequest(initializeRequestHandler)
12
13 Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler)
14 Sys.WebForms.PageRequestManager.getInstance().remove_pageLoaded(pageLoadedHandler)
15
16 Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler)
17 Sys.WebForms.PageRequestManager.getInstance().remove_pageLoading(pageLoadingHandler)
18
19 Method:
20
21 Stops all updates that would occur as a result of an asynchronous postback.
22 Syntax:
23 Sys.WebForms.PageRequestManager.getInstance().abortPostBack();Remarks
24
25 Example:
26 */
27
28 Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
29 Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
30
31 function BeginRequestHandler(sender, args)
32 {
33 var elem = args.get_postBackElement();
34 var snd = sender._postBackSettings.sourceElement.id;
35 ActivateAlertDiv(‘visible‘, ‘AlertDiv‘, snd + ‘ ‘ + elem.value + ‘ processing…‘);
36 }
37
38 function EndRequestHandler(sender, args)
39 {
40 ActivateAlertDiv(‘hidden‘, ‘AlertDiv‘, ”);
41 }
42
43 function ActivateAlertDiv(visstring, elem, msg)
44 {
45 var adiv = $get(elem);
46 adiv.style.visibility = visstring;
47 adiv.innerHTML = msg;
48 }
49
55 </script>
56
Return reference
DataSet providentDS = new DataSet();DataTable tmpDT;
string curCompCode = string.Empty;string CostCenter = string.Empty;//string EmpGroup = string.Empty;
string EmpType = string.Empty;int[] workingYear = {0,0,0};
GetInformationsPF(pinId,ref providentDS,ref curCompCode,ref CostCenter,ref EmpType,ref workingYear);
test
1
<div>
2
<asp:ScriptManager ID=”ScriptManager1″ runat=”server” /> <script type=”text/javascript” language=”javascript”>
3
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
4
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
5
function BeginRequestHandler(sender, args)
6
{
7
var elem = args.get_postBackElement();
8
ActivateAlertDiv(‘visible‘, ‘AlertDiv‘, elem.value + ‘ processing…‘);
9
}
10
function EndRequestHandler(sender, args)
11
{
12
ActivateAlertDiv(‘hidden‘, ‘AlertDiv‘, ”);
13
}
14
function ActivateAlertDiv(visstring, elem, msg)
15
{
16
var adiv = $get(elem);
17
adiv.style.visibility = visstring;
18
adiv.innerHTML = msg;
19
}
20
</script><asp:UpdatePanel ID=”UpdatePanel1″ UpdateMode=”Conditional” runat=”Server”>
21
<ContentTemplate>
22
<asp:Panel ID=”Panel1″ runat=”server” GroupingText=”Update Panel”>
23
Last update:
24
<%= DateTime.Now.ToString()%>.
25
<br />
26
<asp:Button runat=”server” ID=”Button1″ Text=”Process 1″ OnClick=”ProcessClick_Handler” />
27
<asp:Button runat=”server” ID=”Button2″ Text=”Process 2″ OnClick=”ProcessClick_Handler” />
28
</asp:Panel>
29
</ContentTemplate>
30
Sys.WebForms.PageRequestManager.getInstance()
http://www.asp.net/ajax/documentation/live/ClientReference/Sys.WebForms/PageRequestManagerClass/PageRequestManagerPageLoadedEvent.aspx
http://www.asp.net/learn/ajax-videos/video-172.aspx
http://www.asp.net/learn/ajax-videos/video-165.aspx
เอา JavaScript ไว้หลัง tag <asp:ScriptManager ID=”ScriptManager1″ />
<%@ Page Language=“C#“ %><!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”><script runat=”server”>protected void ProcessClick_Handler(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(2000);
}</script><html xmlns=”http://www.w3.org/1999/xhtml”>
<head id=”Head1″ runat=”server”>
<title>PageRequestManager beginRequest Example</title>
<style type=”text/css”>
body {
font-family: Tahoma;
}
div.AlertStyle
{
background-color: #FFC080;
top: 95%;
left: 1%;
height: 20px;
width: 270px;
position: absolute;
visibility: hidden;
}
</style>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<asp:ScriptManager ID=”ScriptManager1″ runat=”server” /> <script type=”text/javascript” language=”javascript”>
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
function BeginRequestHandler(sender, args)
{
var elem = args.get_postBackElement();
ActivateAlertDiv(‘visible‘, ‘AlertDiv‘, elem.value + ‘ processing…‘);
}
function EndRequestHandler(sender, args)
{
ActivateAlertDiv(‘hidden‘, ‘AlertDiv‘, ”);
}
function ActivateAlertDiv(visstring, elem, msg)
{
var adiv = $get(elem);
adiv.style.visibility = visstring;
adiv.innerHTML = msg;
}
</script><asp:UpdatePanel ID=”UpdatePanel1″ UpdateMode=”Conditional” runat=”Server”>
<ContentTemplate>
<asp:Panel ID=”Panel1″ runat=”server” GroupingText=”Update Panel”>
Last update:
<%= DateTime.Now.ToString()%>.
<br />
<asp:Button runat=”server” ID=”Button1″ Text=”Process 1″ OnClick=”ProcessClick_Handler” />
<asp:Button runat=”server” ID=”Button2″ Text=”Process 2″ OnClick=”ProcessClick_Handler” />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<div id=”AlertDiv” class=”AlertStyle”>
</div>
</div>
</form>
</body>
</html>
ref: http://asp.net/ajax/documentation/live/Tutorials/CreateSimpleAJAXApplication.aspx
Handle error in client side
<script type=”text/javascript”>
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(requestEndHandler );
// This function will handle the end request event
function requestEndHandler(sender, args) {
if( args.get_error() ){
document.getElementById(“errorMessageLabel”).innerText =
args.get_error().description;
args.set_errorHandled(true);
}
}
</script>
…
<div id=”errorMessageLabel”></div>
…
Debug javascript edit, debug, and monitor CSS, HTML, and JavaScript live in any web page.
Firebug integrates with Firefox to put a wealth of web development tools at your fingertips while you browse. You can edit, debug, and monitor CSS, HTML, and JavaScript live in any web page.
Just the way you like it
Firebug is always just a keystroke away, but it never gets in your way. You can open Firebug in a separate window, or as a bar at the bottom of your browser.
Sys.WebForms.PageRequestManagerTimeoutException - The server request timed out - error
Last week I had a problem when I tried to export an image from the AGS 9.2 to a pdf file. I got this error: “Sys.WebForms.PageRequestManagerTimeoutException The server request timed out” from the Ajax Extension framework. Its occurred because the export operation was relatively long (something like 2 minutes) and the Ajax Extension Callback framework had a timeout.
How to solve this problem?
To solve this problem we can increase the timeout. You can change the timeout time by adding a new property to the script manager control. This property is called AsyncPostBackTimeOut and it can help us to define the number of seconds before the request will throw a request timeout exception*.
For example if you want that the timeout will take maximum 10 minutes your code should be look like this:
<asp:ScriptManager ID=”ScriptManager1″ runat=”server”
AsyncPostBackTimeOut=”600″ >
</asp:ScriptManager>
* The default value of the AsyncPostBackTimeOut property is 90 seconds.
Javascript popup center
<script language=“javascript“>
![]()
/*
Auto center window script- Eric King (http://redrival.com/eak/index.shtml)
Permission granted to Dynamic Drive to feature script in archive
For full source, usage terms, and 100’s more DHTML scripts, visit http://dynamicdrive.com
*/
![]()
var win = null;
function NewWindow(mypage,myname,w,h,scroll){
LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
settings =
‘height=‘+h+‘,width=‘+w+‘,top=‘+TopPosition+‘,left=‘+LeftPosition+‘,scrollbars=‘+scroll+‘,resizable‘
win = window.open(mypage,myname,settings)
}
![]()
</script>




Auto center window script- Eric King (http://redrival.com/eak/index.shtml) 