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

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube