Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UpdatePanel Exception Handling

Where exceptions occur in the UpdatePanels I've implemented in the ASP.NET web app I'm building, they cause a JavaScript error on the page with some high level error output available in the alert. This is OKish for development, but as soon as the system is in Production, it's obviously no good for multiple reasons. I can surround troublesome activities with Try Catch etc to control the Javascript error but in some cases, I want to take actions on the main page etc to support the user experience.

How do I go about handling errors that occur in UpdatePanels gracefully to provide a seamless and Javascript error free implementation?

like image 238
Chris Avatar asked Dec 01 '09 12:12

Chris


People also ask

What is the use of UpdatePanel?

UpdatePanel controls are a central part of AJAX functionality in ASP.NET. They are used with the ScriptManager control to enable partial-page rendering. Partial-page rendering reduces the need for synchronous postbacks and complete page updates when only part of the page has to be updated.

How is an error handled by default during an asynchronous postback?

With asynchronous postbacks and partial rendering, an exception can go back to the script that initiated the callback, and the default behavior is to show the user the error message. This happens even if you have custom errors enabled for your application.

What is trigger in UpdatePanel?

Triggers for a given UpdatePanel, by default, automatically include any child controls that invoke a postback, including (for example) TextBox controls that have their AutoPostBack property set to true.

What happens when a button placed in the UpdatePanel control is clicked?

The UpdatePanel control contains a Button control that refreshes the content inside the panel when you click it. By default, the ChildrenAsTriggers property is true. Therefore, the Button control acts as an asynchronous postback control.


2 Answers

You can use a combination of the AsyncPostBackError event on the ScriptManager (server-side) and the EndRequest event on the PageRequestManager (client-side) to fully handle server-side errors when using the UpdatePanel.

Here are a couple resources that should help you:

Customizing Error Handling for ASP.NET UpdatePanel Controls

Error Handling Customization for ASP.NET UpdatePanel

Here's a simple example:

// Server-side
protected void ScriptManager1_AsyncPostBackError(object sender, 
    AsyncPostBackErrorEventArgs e)  {
    ScriptManager1.AsyncPostBackErrorMessage =
        "An error occurred during the request: " +
        e.Exception.Message; 
}


// Client-side
<script type="text/javascript">
  function pageLoad()  {
    Sys.WebForms.PageRequestManager.getInstance().
        add_endRequest(onEndRequest);  
  }

  function onEndRequest(sender, args)  {
    var lbl = document.getElementById("Label1");
    lbl.innerHTML = args.get_error().message;
    args.set_errorHandled(true);
  }
</script>
like image 183
Rob Windsor Avatar answered Sep 30 '22 17:09

Rob Windsor


If you just want to fix the browser javascript error and display the exception message to the user, you just need to add this to your Masterpage somewhere after the form declaration:

<!-- This script must be placed after the form declaration -->
<script type="text/javascript">
    Sys.Application.add_load(AppLoad);

    function AppLoad() {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);
    }

    function EndRequest(sender, args) {
        // Check to see if there's an error on this request.
        if (args.get_error() != undefined) {

            var msg = args.get_error().message.replace("Sys.WebForms.PageRequestManagerServerErrorException: ", "");

            // Show the custom error. 
            // Here you can be creative and do whatever you want
            // with the exception (i.e. call a modalpopup and show 
            // a nicer error window). I will simply use 'alert'
            alert(msg);

            // Let the framework know that the error is handled, 
            //  so it doesn't throw the JavaScript alert.
            args.set_errorHandled(true);
        }
    }
</script>

You do not need to catch the OnAsyncPostBackError even unless you want to customize the message. Go to my blog post if you want more information about this.

like image 22
Wagner Danda da Silva Filho Avatar answered Sep 30 '22 18:09

Wagner Danda da Silva Filho