Wahlin on .NET
Coping With Click-Happy Users
The PageRequestManager client-side class provides a rich-event model to help you deal with impatient users.
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 MVP for ASP.NET and XML Web Services) is the founder of The Wahlin Group which specializes in .NET and SharePoint onsite, online and video training and consulting solutions. Dan also founded the XML for ASP.NET Developers Web site, which focuses on using ASP.NET, XML, AJAX, Silverlight and Web Services in Microsoft's .NET platform. He's also on the INETA Speaker's Bureau and speaks at conferences and user groups around the world. Dan has written several books on .NET including "Professional Silverlight 2 for ASP.NET Developers," "Professional ASP.NET 3.5 AJAX, ASP.NET 2.0 MVP Hacks and Tips," and "XML for ASP.NET Developers." Read Dan's blog here.