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

  • Full Stack Hands-On Development with .NET

    In the fast-paced realm of modern software development, proficiency across a full stack of technologies is not just beneficial, it's essential. Microsoft has an entire stack of open source development components in its .NET platform (formerly known as .NET Core) that can be used to build an end-to-end set of applications.

  • .NET-Centric Uno Platform Debuts 'Single Project' for 9 Targets

    "We've reduced the complexity of project files and eliminated the need for explicit NuGet package references, separate project libraries, or 'shared' projects."

  • Creating Reactive Applications in .NET

    In modern applications, data is being retrieved in asynchronous, real-time streams, as traditional pull requests where the clients asks for data from the server are becoming a thing of the past.

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

Subscribe on YouTube