Click here to Skip to main content
13,589,661 members
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 15 Jan 2009
Licenced CPOL

Error Handling Customization for ASP.NET UpdatePanel

, 21 Jan 2009
Rate this:
Please Sign up or sign in to vote.
How to customize an UpdatePanel client side error that is presented to the user.


When an error (application internal error or lost server connection) occurs during partial-page updates in UpdatePanel controls, the default behavior is a browser message box displaying an error message like this:


It is possible to customize this behavior and give more details to the user, or to display the error, but not with a message box, but with an error panel inside your page.

Using the Code

Within the attached project, I make a sample page with ScriptManager and UpdatePanel inside a Button that throws an exception on click.

If a CheckBox is checked, the client script that customizes error handling will be enabled, so you can see customization behavior that shows a popup with a custom message and redirects the error message to the error panel inside the page. Otherwise, you can see the browser default behavior that will display the browser message box. In order to use it, open the project and start without debugging (Ctrl+F5).


This is the JavaScript needed to handle the UpdatePanel error:

    <script type="text/javascript">
        function EndRequestHandler(sender, args) {
            //if UpdatePanel error occurs...
            if (args.get_error() != undefined) {
                var Error = "3-add further description client side... : [Code]:" +
                    args.get_response().get_statusCode() + "  [Message]: " +
                //Show your custom popup or...
                //Hide default ajax error popup
                //...redirect error to your Error Panel on page
                document.getElementById("Label1").innerText = Error;

    <script type="text/javascript">

But, I only want to add it to the page if the checkbox is enabled, so I do that from the code-behind.

if (CheckBox1.Checked)
    if (!Page.ClientScript.IsClientScriptBlockRegistered("EndRequestHandler"))
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append("<script type=\"text/javascript\">");
                sb.Append("function EndRequestHandler(sender, args) {");
                sb.Append("if (args.get_error() != undefined) {");
                   "var Error = \"3-add further description client side... : [Code]:\" +
                   args.get_response().get_statusCode() + \"  [Message]: \" +
                //Show your custom popup or...
                //Hide default ajax error popup
                //...redirect error to your Error Panel on page
                sb.Append("document.getElementById(\"Label1\").innerText = Error;");
                    "EndRequestHandler", sb.ToString(), false);

    if (!Page.ClientScript.IsStartupScriptRegistered("AddEndRequestHandler"))
                System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
                sb2.Append("<script type=\"text/javascript\">");
                      "AddEndRequestHandler", sb2.ToString(), false);

Points of Interest

When an exception occurs, an error message will be concatenated starting from the origin of the exception ("1-Internal Error...", in our case), and then passing through ScriptManager1_AsyncPostBackError, so you can add further description to show to the client side.

protected void Button1_Click(object sender, EventArgs e)
    throw new Exception("1-Internal Error...");

protected void ScriptManager1_AsyncPostBackError(object sender,
    AsyncPostBackErrorEventArgs e)
    //2- Add further description to e.Exception.Message that contains
    //"1-Internal Error..."
    ScriptManager1.AsyncPostBackErrorMessage = e.Exception.Message +
        " 2-add further description server side...";
    //now message error is "1-Internal Error... 2-add further description
    //server side..." 

If the error handling client side is not managed, this message will be shown as is, "1-Internal Error... 2-add further description server side..."; otherwise, you can add further description by using the script:

    var Error = "3-add further description client side... : [Code]:" +
        args.get_response().get_statusCode() + "  [Message]: " +

and then show it with an alert, or redirect to a page control, and the output will be:


The following is a list of Web Server Status Code Definitions (1xx through 5xx), that typically occur for an internal application error: 10 Status Code Definitions.

The following is a list of network error codes returned by the WinInet functions (12001 through 12156), which typically occur when a web server is unreachable: INFO: WinInet Error Codes (12001 through 12156).


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


About the Author

Software Developer
Italy Italy
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralAlready an article... Pin
Sandeep Mewara21-Jan-09 18:57
memberSandeep Mewara21-Jan-09 18:57 
GeneralRe: Already an article... Pin
aphazel16-Nov-09 3:35
memberaphazel16-Nov-09 3:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02-2016 | 2.8.180615.1 | Last Updated 21 Jan 2009
Article Copyright 2009 by msp.netdev
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid