|
Coping With Click-Happy Users
The PageRequestManager client-side class provides a rich-event model to help you deal with impatient users.
by Dan Wahlin
July 10, 2007
It's no secret that using AJAX technologies to build your Web pages allows end users to receive data faster. Users can view page results, sort data and refresh data in a rich environment that feels more like a Windows application. Although this is certainly desirable, it can lead to click–happy users who constantly refresh pages or manipulate paging controls without regard for the load they're placing on the Web server or database.
When users trigger multiple asynchronous requests in ASP.NET AJAX Web pages, the last request takes precedence over the previous requests. However, the previous requests still consume server resources even though their response data isn't used. Fortunately, the PageRequestManager client-side class provides a rich event model that you can use to deal with these click-happy users and reduce your server load.
In my last column, I introduced events exposed by the PageRequestManager class and demonstrated how you can use these events to animate an UpdatePanel as an asynchronous postback occurs, providing users with visual cues about the progress of their request. In this column, I'll take this process one step further. I'll show you how PageRequestManager events can be used to cancel AJAX requests and notify users to wait for their initial request to complete before making subsequent requests.
The key to cancelling asynchronous postback requests is the PageRequestManager's initializeRequest event. The initializeRequest event is raised when an asynchronous postback request is first initialized, but before it is actually sent to the server. By attaching the PageRequestManager's initializeRequest event to an event handler, you can cancel successive requests made by an impatient user.
Listing 1 shows you how to hook the PageRequestManager's initializeRequest event to an event handler named InitRequest() as the application initializes.
When the initializeRequest event is raised, two objects are passed to the InitRequest() event handler: the sender and an InitializeRequestEventArgs object. Similarly, the InitializeRequestEventArgs object exposes two key properties: postBackElement and cancel. The postBackElement property identifies the DOM element that raised the event (such as a refresh button), and the cancel property can be used to cancel a request.
Listing 2 demonstrates how you can use these properties in conjunction with the PageRequestManager's isInAsyncPostBack property to prevent multiple requests from being made before an initial request returns from the server. If a request is in process and a user clicks on a button with an ID of btnRefresh again, then a message will tell the user to wait. Figure 1 shows you what users will see when they click the refresh button too quickly.
By using PageRequestManager events, you can perform a variety of tasks, including displaying animated visual cues and cancelling asynchronous postback requests. In my next column, I'll outline how you can abort AJAX requests that have already been submitted to the server.
About the Author
Dan Wahlin (Microsoft Most Valuable Professional for ASP.NET and XML Web Services) is a .NET development instructor at . Dan founded the XML for , which focuses on using ASP.NET, XML, AJAX and Web Services in Microsoft's .NET platform. He’s also on the INETA Speaker's Bureau and speaks at several conferences. Dan has co-authored/authored several different books on .NET including ASP.NET 2.0 MVP Hacks, Professional ASP.NET AJAX and XML for ASP.NET Developers. When he's not writing code, articles or books, Dan enjoys writing and recording music and playing golf and basketball with his wife and kids. Dan blogs at and .
Back to top
|