Wahlin on .NET

Abort AJAX Requests

Add the PageRequestManager class's built-in abort capabilities to Web apps that allow users to make longer-running requests.

Asynchronous postback requests are typically speedy operations — given that AJAX technology aims to enhance the efficiency of an application. However, specific types of requests, such as those that generate reports, query Web services or call into third-party systems, may take longer to complete. The speed at which these types of operations perform depends on a variety of factors, including network speed, the amount of data being queried and the location of the data.

While they wait for longer-running requests to complete, users may decide that they don't need the requested data. This presents you with a challenge. Aborting requests isn't common in Web applications as it is in desktop applications or OSs. More specifically, users are accustomed to cancelling desktop-search operations, but they aren't accustomed to cancelling Web requests because non-AJAX Web applications don't allow for it while pages are loading. Clicking the browser's stop button is as close as a user gets to truly aborting a request.

However, AJAX-enabled applications lend themselves well to aborting requests since only parts of pages are updated instead of entire pages. The ASP.NET AJAX script library provides a simple mechanism for aborting requests that you can tap into without writing a lot of client-side code.

In last few columns, you have learned several different ways that the PageRequestManager class can be used. You can use it to perform tasks that range from animating UpdatePanels to cancelling asynchronous postback requests while earlier requests are being processed. Nearly all of these techniques require that you tie into various PageRequestManager events, such as initializeRequest, pageLoaded or endRequest. In contrast, to abort requests while they're being processed, you can use the PageRequestManager class without writing code to handle events.

First, you must access an instance of the PageRequestManager object using its static getInstance() method. Once the instance is available, you can check its isInAsyncPostBack property to see if an asynchronous postback operation is currently running. If a process is running, you can call the PageRequestManager class's abortPostBack() method to abort the request. Listing 1 shows an example of how to perform these steps when an abort button is clicked by a user while an asynchronous operation is being processed.

Many AJAX-enabled Web pages perform speedy asynchronous postback operations that don't need to be aborted. However, applications that allow users to make longer-running requests benefit from the PageRequestManager class's built-in abort capabilities, and you can easily add this feature.

In my next article, I'll switch gears from PageRequestManager functionality and begin discussing how you can leverage Web services with ASP.NET AJAX applications.

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.

comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.