UpdatePanel Exception Handling UpdatePanel Exception Handling asp.net asp.net

UpdatePanel Exception Handling


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-sideprotected 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>


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.


Could you override the page level error method, catch the exception and handle how you see fit.

protected override void OnError(EventArgs e){    //show error message here}