Click here to Skip to main content
12,700,966 members (28,326 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked

Handling Ajax Errors in ASP.NET

, 21 May 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
How to handle Ajax errors in ASP.NET


Yesterday, I wrote about issue with Ajax postbacks in an application I am working currently. The problem was with buttons that was triggering UpdatePanels to update asynchronously. After adding loading indicator, I wanted to add error handling at client side.

As loading was handled by PageRequestManager events, I was trying to achieve errors in the same way. At first, I created some HTML to show when error happens:

<div id="errorDialog" class="hidden">
    <div class="ui-widget center height100per width100per">
        <div class="ui-state-error ui-corner-all" style="padding: 0 .7em;">
                <span class="ui-icon ui-icon-alert" style="float: left; 
                	margin-right: .3em;"></span>
                <br />
                <span id="errorDesc"></span></p>

It creates Alert container from jQueryUI theme as you can see here. For me, it looks like this:

Styles are mostly from this library. My classes have meaningful names and are defined:


Folks at Microsoft do not add special event for errors in PageRequestManager, args argument of event handler for endRequest handler can tell as if something bad happened inside our application:

pageManager.add_endRequest(function (sender, args) {
        if (args.get_error() != undefined) {

Now we have to just add some logic to show the error message inside our error container:

pageManager.add_endRequest(function (sender, args) {
        if (args.get_error() != undefined) {
            var errorMessage = args.get_error().message;
            jQuery('#errorDesc').html(htmlEncode(errorMessage).replace(":", "<br />"));
                width: 600,
                height: 300
        function htmlEncode(value) {
            return jQuery('<div/>').text(value).html();

htmlEncode function replaces special HTML characters like < or > with HTML entities. It works by creating in-memory-only div element, and then setting some HTML as text and getting this as HTML. Simple as that. I added replacing : with HTML next line to show error description from C# in the below summary of exception. After that dialog is opening to show to user that something bad happens.

Dialog is much bigger than it needs to be, but I think that user must know that it's something serious and bigger message helps with that. Smile

That's all!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Poland Poland
No Biography provided

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 22 May 2013
Article Copyright 2013 by n.podbielski
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid