Conflict between OnError and OnAsyncPostBackError events

Last post 01-22-2008 11:09 AM by zachbaker. 9 replies.

Sort Posts:

  • Conflict between OnError and OnAsyncPostBackError events

    12-30-2006, 7:41 PM

    I am updating my application from ATLAS to Ajax RC.

    All my pages are inherited from my own WebPage subclass page that implements an OnError event and I have included the Script Manager with the OnAsyncPostBackError property in the master pages of the application.

    When an exception is thrown within an Update Panel the Onerror event captures the exception and I haven't found a way to setup the AsyncPostBackErrorMessage. An alert message appears with the text:

     
    Sys.WebForms.PageRequestManagerServerException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500

     
    Any suggestions...?

     
    Thanks....Antonio
     

  • Re: Conflict between OnError and OnAsyncPostBackError events

    01-08-2007, 9:57 PM
    Answer
    Try to capture asynchronous post back error as the follow.
         ScriptManager.AsyncPostBackError += new EventHandler<AsyncPostBackErrorEventArgs>(ScriptManager_AsyncPostBackError);
    Determine if it is asynchronous post back:
         ScriptManager.IsInAsyncPostBack;
    Get synchronous post back error message:
         ScriptManager.AsyncPostBackErrorMessage;
    Get asynchronous post back source:
         ScriptManager.AsyncPostBackSourceElementID;
    Wish the above can help you.
    Sincerely,
    JiCheng Wang
    Microsoft Online Community Support

    This posting is provided "AS IS" with no warranties, and confers no rights.
  • Re: Conflict between OnError and OnAsyncPostBackError events

    01-25-2007, 3:13 PM
    • Member
      2 point Member
    • pheilmann
    • Member since 11-28-2006, 2:32 PM
    • Jacksonville, Fl
    • Posts 16

    I am having the same issue...  We have existing sites that uses a standard basepage the overrides the OnError to catch any page errors.  It will log the error and then redirect to a nicely formatted error page to explain the error.  This works fine, but when I am inside a AJAX call and I get an error, I also get the following error

    Sys.WebForms.PageRequestManagerServerException: An unknown error occurred while processing the request on the server. The status code returned from the server was: 500

     But if I remove my OnError Handler completely, then I will get the actual error in the javascript popup.  How can I handle both the normal page errors and AJAX errors if just having the OnError seems to kill the AsyncPostBackError info???

     Pat

  • Re: Conflict between OnError and OnAsyncPostBackError events

    02-23-2007, 10:20 AM
    • Member
      6 point Member
    • lea2008
    • Member since 02-23-2007, 3:13 PM
    • Posts 3

    changing UpdateMode from "Conditional" to "Always" solved my problem

  • Re: Conflict between OnError and OnAsyncPostBackError events

    06-05-2007, 4:02 PM

    I tried many ways to raise and catch the error with both OnError and OnAsyncPostBackError implemented. but no use. It always go to OnError handler.

    Is someone suggests the complete code ... with master page, update panels on a page and onError and OnAsyncPostBackError events implemented?

    Basically, I am looking to implement both handlers and distinguish to redirect Page request errors to OnError and Update panel requests errors to OnAsyncPostBackError;

    Any help is appreciated in advance.

  • Re: Conflict between OnError and OnAsyncPostBackError events

    08-24-2007, 4:50 PM
    • Member
      10 point Member
    • NWGaEagle
    • Member since 03-13-2007, 7:49 PM
    • Posts 7

    I found a way to do this ...

    Put this snippet of code in your base Page codebehind ... this should capture every error that gets thrown.

    1    protected override void OnError(EventArgs e)
    2            {
    3                ScriptManager mgr = ScriptManager.GetCurrent(this);
    4    
    5                System.Exception ex = this.Server.GetLastError();
    6    
    7                if (mgr.IsInAsyncPostBack)
    8                {
    9                    base.OnError(e);
    10               }
    11               else
    12               {
    13                   this.Server.ClearError();
    14   
    15                   base.OnError(e);
    16   
    17                   Response.Write(<html><head><title>Error</title></head><body>");
    18                   Response.Write("<table class='Section'><tr class='SectionTitle'><td colspan='2'>Error Details</td></tr><tr><TD valign='top' class='FieldLabel' width='125px'>Error Message:</TD><td class='GenText'>");
    19                   Response.Write(ex.Message);
    20                   Response.Write("</TD></TR><TR><TD valign='top' class='FieldLabel'>Error Source:</TD><TD class='GenText'>");
    21                   Response.Write(ex.Source);
    22                   Response.Write("</TD></TR><TR><TD valign='top' class='FieldLabel'>Stack Trace:</TD><TD class='GenText'>");
    23                   Response.Write(ex.StackTrace);
    24                   Response.Write("</TD></TR><TR><TD colspan=\"2\"><a href=\"");
    25                   Response.Write(Request.RawUrl);
    26                   Response.Write("\">Return to previous page</a></td></tr></table></body></html>");
    27                   Response.End();
    28               }
    29           }
    
     

    Then in your master page (or whatever page) codebehind capture the AsyncPostBackError from the ScriptManager tag (making sure to hook the OnAsyncPostBackError in the aspx page to the ScriptManager1_AsyncPostBackError): 

    1            protected void ScriptManager1_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
    2            {
    3                if (e.Exception.Data["ExtraInfo"] != null)
    4                {
    5                    ScriptManager1.AsyncPostBackErrorMessage =
    6                        e.Exception.Message +
    7                        e.Exception.Data["ExtraInfo"].ToString();
    8                }
    9                else
    10               {
    11                   ScriptManager1.AsyncPostBackErrorMessage = "The following error occurred: " + e.Exception.Message;
    13               }
    14           }
    
     

     

  • Re: Conflict between OnError and OnAsyncPostBackError events

    08-24-2007, 6:49 PM

    My updated code for handling OnError and OnAsyncPostBackError events ...  

    I took different approach like assigning event handlers early and then do accordingly. I have all my pages contain a Master Page and derived from Page Base ... MyAppPageBase

    public class MyAppPageBase : Page
    {
           protected override void OnInit(EventArgs e)
           {
                bool AttachPageErrorEventHandler = true;
                
                // Determine if it is a page request, then only attach page error event handler
                try
               
    {
                         
    if (this.Page != null &&
                                        
    ScriptManager.GetCurrent(this.Page) != null &&
                         
                    ScriptManager.GetCurrent(this.Page).IsInAsyncPostBack)
                          {
                                 
    // Note: We are not attaching any event handler for page error -->
                                 
    this.Page.Error -= new EventHandler(MyAppPage_Error);
                                  
    this.Page.Error += new EventHandler(MyAppPage_UP_Error);
                                  AttachPageErrorEventHandler =
    false;
                           }
                 }
                
    catch { }

                 if (AttachPageErrorEventHandler)
                 {
                          
    this.Page.Error += new EventHandler(MyAppPage_Error);
                          
    this.Page.Error -= new EventHandler(MyAppPage_UP_Error);
                 }
                
    base.OnInit(e);
           }

          protected void MyAppPage_Error(object sender, EventArgs e)
          {
             
    Exception exBase = Server.GetLastError().GetBaseException();

              if (exBase.GetType().Name == "HttpRequestValidationException")
              {
                         
    >>> Redirect to a page to show that you are using <script> tag as data; Malformed data
             
    }
             
    else // all other / generic exception
              
    {

                      this.Context.AddError(exBase);
                      Server.ClearError();
                      Server.Transfer(
    "~/Error.aspx");
                      Response.Flush();
    /*The line is added to cancel the event bubbleup */
                     
    Response.End(); /*The line is added to cancel the event bubbleup */
               }
           }

           protected void TPPage_UP_Error(object sender, EventArgs e)
           {
              Exception exBase = Server.GetLastError().GetBaseException();
              if (exBase.GetType().Name == "HttpRequestValidationException")
              {
                     
    string str = "You have submitted the data with <script>tag or with malformed data";
                      Response.Write(
    string.Format("{0}|error|500|{1}|", str.Length, str ));
              }
              
    else // all other types of exception
             
    {
                      Response.Write(
    string.Format("{0}|error|500|{1}|", exBase.Message.Length, exBase.Message));
             
    }
         }

    }

     so far it is working great!

  • Re: Conflict between OnError and OnAsyncPostBackError events

    01-15-2008, 6:52 PM
    • Member
      4 point Member
    • zachbaker
    • Member since 01-15-2008, 6:47 PM
    • Posts 2

     This is great, but one thing I don't see this catching is:

    Sys.WebForms.PageRequestManagerServerErrorException
    An unknown error occured while processing the request on the server.  The status code returned from the server was: 503.

     
    This is the "server unavailable" error.  happens in cases where a server goes down, etc. 

    I have been scratching my head on how to catch this one.  might sound obscure, but it absolutely kills AJAX timers.

  • Re: Conflict between OnError and OnAsyncPostBackError events

    01-22-2008, 12:15 AM

    My 2 cents about your comments ...

    - Most or all 50x errors are not able to catch @ server side
    - Important point is some or most of them get handled @ ISAPI / Web / Http handler events level -> these wont even propogate to page level
    - Sorry to say the error "Sys.WebForms.PageRequestManagerServerErrorException" is a client side exception. You can use firebug to see what you are sending and receiving to see more details about the error while development or fixing. But, for a common user, it is tough.
    - You can modify and add client side validation to handle these errors....

    For ex: If you use Ajax Control kit and populating Cascading dropdown, when you hit the max length in JSON serialization, it returns an 500 error.
                    It is tough to analyze at the page and even at debugging also.

    You can add it, but may not hit for all 500 errors.

    - Sreedhar Vankayala 

  • Re: Conflict between OnError and OnAsyncPostBackError events

    01-22-2008, 11:09 AM
    • Member
      4 point Member
    • zachbaker
    • Member since 01-15-2008, 6:47 PM
    • Posts 2

    I sort of agree.  And Firebug is wonderful despite bugginess with tabbed windows.

    I was able to catch and handle this error client side.

    However, the silly thing is that you will not find PageRequestManagerServerErrorException anywhere in the javascript that is emitted. 

    For those interested, here is what you do.  I've seen postings that ALMOST get this right, but they don't.  You can catch per-status code errors or you can catch the exception message (or both).

    728 <script language="javascript" type="text/javascript">
    729 var prm = Sys.WebForms.PageRequestManager.getInstance();
    738 prm.add_initializeRequest(InitializeRequest);
    739 prm.add_endRequest(EndRequest);

    function EndRequest(sender, args)

    764 if( args.get_error() )
    766 if (args._error.httpStatusCode == 503)
    768 args.set_errorHandled( true );
    769 //site down, need to give it more time.


     

Page 1 of 1 (10 items)