Click here to Skip to main content
15,867,686 members
Articles / Web Development / ASP.NET

Error Handling Customization for ASP.NET UpdatePanel

Rate me:
Please Sign up or sign in to vote.
3.50/5 (5 votes)
21 Jan 2009CPOL2 min read 55.4K   309   26   3
How to customize an UpdatePanel client side error that is presented to the user.

Introduction

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:

defaultmsgerror1.jpg

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).

page.jpg

This is the JavaScript needed to handle the UpdatePanel error:

ASP.NET
...
    <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]: " +
                    args.get_error().message;
                //Show your custom popup or...
                alert(Error);
                //Hide default ajax error popup
                args.set_errorHandled(true);
                //...redirect error to your Error Panel on page
                document.getElementById("Label1").innerText = Error;
            } 
        }
    </script>

...
    <script type="text/javascript">
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequestHandler);
    </script>

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

C#
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) {");
                sb.Append(
                   "var Error = \"3-add further description client side... : [Code]:\" +
                   args.get_response().get_statusCode() + \"  [Message]: \" +
                   args.get_error().message;");
                //Show your custom popup or...
                sb.Append("alert(Error);");
                //Hide default ajax error popup
                sb.Append("args.set_errorHandled(true);");
                //...redirect error to your Error Panel on page
                sb.Append("document.getElementById(\"Label1\").innerText = Error;");
                sb.Append("}");
                sb.Append("}");
                sb.Append("</script>");
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(),
                    "EndRequestHandler", sb.ToString(), false);
    }

    if (!Page.ClientScript.IsStartupScriptRegistered("AddEndRequestHandler"))
    {
                System.Text.StringBuilder sb2 = new System.Text.StringBuilder();
                sb2.Append("<script type=\"text/javascript\">");
                sb2.Append(
                    "Sys.WebForms.PageRequestManager.getInstance().add_endRequest(
                    EndRequestHandler);");
                sb2.Append("</script>");
        Page.ClientScript.RegisterStartupScript(this.GetType(),
                      "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.

C#
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:

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

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

defaultmsgerror2.jpg

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).

License

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


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
shmihir10-Dec-10 2:45
shmihir10-Dec-10 2:45 

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.