Programming Microsoft ASP.NET 4 - Dino Esposito [454]
Figure 20-3. A progress template informing users that some work is being done.
Note that the UpdateProgress control is not designed to be a gauge component, but rather a user-defined panel that the ScriptManager control shows before the panel refresh begins and that it hides immediately after its completion.
Important
If you’re looking for a real gauge bar to monitor the progress of a server-side task, partial rendering and the UpdateProgress control are not the right tools. As you’ll see later in the chapter, polling is one of the main drawbacks of partial rendering and polling is unavoidable for monitoring server tasks from the client.
Client-Side Events for Richer Feedback
Each asynchronous postback is triggered on the client via script. The entire operation is conducted by the PageRequestManager client object, which invokes, under the hood, the XMLHttpRequest object. What kind of control do developers have on the underlying operation? If you manage XMLHttpRequest directly, you have full control over the request and response. But when these key steps are managed for you, there’s not much you can do unless the request manager supports an eventing model.
The Sys.WebForms.PageRequestManager object provides a few events so that you can customize the handling of the request and response. Table 20-7 lists the supported events that signal the main steps around an Ajax postback that partially update a page. The events are listed in the order in which they fire to the client page.
Table 20-7. Properties of the UpdateProgress Control
Event
Event Argument
Description
initializeRequest
InitializeRequestEventArgs
Occurs before the request is prepared for sending
beginRequest
BeginRequestEventArgs
Occurs before the request is sent
pageLoading
PageLoadingEventArgs
Occurs when the response has been acquired but before any content on the page is updated
pageLoaded
PageLoadedEventArgs
Occurs after all content on the page is refreshed as a result of an asynchronous postback
endRequest
EndRequestEventArgs
Occurs after an asynchronous postback is finished and control has been returned to the browser
To register an event handler, you use the following JavaScript code:
var manager = Sys.WebForms.PageRequestManager.getInstance();
manager.add_beginRequest(OnBeginRequest);
The prototype of the event handler method—OnBeginRequest in this case—is shown here:
function beginRequest(sender, args)
The real type of the args object, though, depends on the event data structure. By using any of these events, you can control in more detail the steps of an asynchronous request. Let’s dig out more.
The initializeRequest event is the first in the client life cycle of an asynchronous request. The life cycle begins at the moment a postback is made that is captured by the UpdatePanel’s client-side infrastructure. You can use the initializeRequest event to evaluate the postback source and do any additional required work. The event data structure is the InitializeRequestEventArgs class. The class features three properties: postBackElement, request, and cancel.
The postBackElement property is read-only and evaluates to a DomElement object. It indicates the DOM element that is responsible for the postback. The request property (read-only) is an object of type Sys.Net.WebRequest and represents the ongoing request. Finally, cancel is a read-write Boolean property that can be used to abort the request before it is sent.
Immediately after calling the initializeRequest handler, if any, the PageRequestManager object aborts any pending async requests. Next, it proceeds with the beginRequest event and then sends the packet.
When the response arrives, the PageRequestManager object first processes any returned data and separates hidden fields, updatable panels, and whatever pieces of information are returned from the server. Once the response data is ready for processing,