Labels

Tuesday, February 3, 2009

Handling Errors in AJAX

Hi,

 

This blog post summarizes a few approaches of Error Handling in AJAX.

 

Error handling in an AJAX world can often be tricky -- especially when AJAX call-backs are taking place and a mixture of client and server code is running within an application.

There are three approaches.

 

 

·          Handle the "OnAsyncPostBackError" event on the ScriptManager control - To catch and/or modify an error message that occurs during the processing of an AJAX postback callback on the server. 

 

·          Handle client-side JavaScript events on the page to intercept any error message sent back from the server, and perform custom client-side actions as a result.

 

·          Handle the "AllowCustomErrors" property of the ScriptManager control. This enables the standard ASP.NET custom error support to be enabled to redirect a user automatically to an error page when an error occurs (even if it is happening during an AJAX postback).

 

When you set this property to true, you're saying that ScriptManager should check for a custom error page associated with the current error an show it to the user. To utilize it required is to add in the web.config file.

 

<customErrors mode="On">

 
     <error statusCode="404" redirect ="error.aspx"/>

 
</customErrors>

 

 

 

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        int res = 1/0;
    }
    catch (Exception ex)
    {       
        ex.Data["ExtraInfo"] = " You can't divide by 0";
        throw ex;
    }        
}

 

<asp:ScriptManager ID="ScriptManager1" runat="server" OnAsyncPostBackError=" MyAsyncPostBackError">
</asp:ScriptManager>
 
 
protected void MyAsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
    if (e.Exception.Data["ExtraInfo"] != null)
    {
        ScriptManager1.AsyncPostBackErrorMessage = e.Exception.Message +            
        e.Exception.Data["ExtraInfo"].ToString();
    }
    else
    {
        ScriptManager1.AsyncPostBackErrorMessage = "An unspecified error occurred.";
    }
}

 

 

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        int res = 1/0;
    }
    catch (Exception ex)
    {       
      ex.Data["ExtraInfo"] = "Explicitly raised Exception";
      throw ex;
    }        
}

 

<asp:ScriptManager ID="ScriptManager1" runat="server">  
    </asp:ScriptManager>
 
    <script language="javascript" type="text/javascript">
        var prm = Sys.WebForms.PageRequestManager.getInstance();        
        prm.add_endRequest(EndRequest);        
        
                
        function EndRequest(sender, args)
        {
           if (args.get_error() != undefined)
           {
               var errorMessage;
               if (args.get_response().get_statusCode() == '200')
               {
                   errorMessage = args.get_error().message;
               }
               else
               {
                   // Error occurred somewhere other than the server page.
                   errorMessage = 'An unspecified error occurred. ';
               }
               args.set_errorHandled(true);
               ActivateAlertDiv('visible',errorMessage);
               window.setTimeout("ActivateAlertDiv('hidden','')", 2000);
           }
 
           //ActivateAlertDiv('hidden','Previous request is still in Progress');
        }
        
        function ActivateAlertDiv(visString,msg)
        {            
             var adiv = $get('AlertDiv');
             var aspan = $get('AlertMessage');
             adiv.style.visibility = visString;
             aspan.innerHTML = msg;
        }        
       
        if(typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();
   </script>
 
protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        int res = 1/0;
    }
    catch (Exception ex)
    {       
      ex.Data["ExtraInfo"] = "Explicitly raised Exception";
      throw ex;
    }        
}

 

<asp:ScriptManager ID="ScriptManager1" runat="server" AllowCustomErrors="true" >
</asp:ScriptManager>
 
 
<customErrors mode="On">

  
     <error statusCode="404" redirect ="error.aspx"/>

  
</customErrors>
 

 

 

 

 

 

 

Thanks & Regards,

Arun Manglick || Senior Tech Lead

No comments:

Post a Comment