Tip/Trick: Handling Errors with the UpdatePanel control using ASP.NET AJAX

Luis Abreu is an ASP.NET MVP who has a great blog on the http://msmvps.com blog site.  Earlier today he posted a great tutorial post that describes how to use some of the new features in the ASP.NET AJAX Beta1 release to add more robust error handling into your application.  I highly recommend reading and bookmarking it for future use.

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.  In its most recent release, the <asp:updatepanel> control that comes with ASP.NET AJAX now has much more robust error handling and reporting features for you to use.  Specifically:

1) You can now handle the "OnAsyncPostBackError" event on the <asp:scriptmanager> control to catch and/or modify an error message that occurs during the processing of an AJAX postback callback on the server. 

2) You can now set the "AllowCustomErrors" property on the <asp:scriptmanager> control to enable 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).

3) You can now optionally 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 (for example: to output the error message to a nicely formatted <div> section instead of performing a pop-up message). 

Read all about how to take advantage of the above new features from Luis' great tutorial here.

Hope this helps,

Scott

34 Comments

  • great link to a good post. Till now I used to see the JavaScript alert box in time of errors

  • Great post, thanks Scott!

    Saves our team tons of time figure out how to do this :D

  • Hi Daniel,

    Can you send me an email with more details on this (scottgu@microsoft.com)? I will then loop you in with some folks on the ASP.NET AJAX team to help investigate and figure out what is going wrong.

    Thanks,

    Scott

  • Wow Scott, do you sleep sometime? I'm looking at your posts, and the hours you post it, I think you drink a lot of coffee.

  • Hi Caleb,

    I try not to sleep -- it is very time inefficient. :-)

    Thanks,

    Scott

  • I too am experiencing the same symptoms with Beta 1. Firefox works well, but IE 6, sp1 gets 'sys' is undefined the first time the page is attempted to be loaded. Clicking 'refresh' in IE and upon page reload, the page loads fine.

    Any help is greatly appreciated.

  • Hi Wayne,

    Can you send me email (scottgu@microsoft.com) with more details about this issue?

    We've seen a few reports of this and are trying to track down exactly what is causing it (right now it look like a possible patch issue within IE).

    Thanks,

    Scott

  • Having the same problem, tested on 4 computers, with IE6 (with all critical patches), IE7, and FireFox.

  • Scott,

    I downloaded the Beta 2 Atlas Extensions, which work great.

    I download the Beta 2 Ajax Control Toolkit (nosource) package and installed it
    .
    When opening the sample website with Visual Studio 2005, it failed.
    The error was about assembly could not be loaded, etc.

    I removed the original AjaxControlkit.dll from the project, downloaded the Ajax Control kit full, copied the dll out of that one, and added it to my project.

    When I run it now, the sample website loads, but clicking any demo control fails with an error telling sys is undefined.


    Any solutions ?

    Greets
    Arnout Symoens

  • I am also experiencing the IE6 'sys is undefined ' issue (no problem in Firefox). Google tells me that I am not alone. Do you have an update on the status of this problem?

  • Hi Jason,

    The Beta2 release requires a new entry in the web.config file, and this sample was built using Beta1.

    My blog post here describes the entry to add: http://weblogs.asp.net/scottgu/archive/2006/11/08/ASP.NET-AJAX-1.0-Beta-2-Release.aspx

    Once you do this the sample should work fine.

    Thanks,

    Scott

  • Hi Arnout,

    Any chance you could post this question on the ASP.NET AJAX Control Toolkit forum on http://forums.asp.net

    The team monitors this forum pretty closely and should be able to help.

    Thanks,

    Scott

  • Scott I assume that you are referring to the section indicating the need to add an entry for ScriptResource.axd in your 'ASP.NET AJAX 1.0 Beta 2 Release' posting. We did make this change during the upgrade to beta 2 but we still receive this error with IE 6 (but not with IE 7 or FF)

  • Hi Jason,

    Can you send me email with more details about this? I'd like to them loop you in with a few folks to help investigate and figure out what is going wrong.

    Thanks,

    Scott

  • I started getting the error when installed Beta 2. No problems in Beta 1. Interestingly, on my local machine its working fine. Then another developer ran into this problem on his machine, and now finally when i pushed everything to QA I am running into the error as well. 'Sys' is undefined..

  • Hi Sam,

    The issue you are running into is because the web.config file in the sample needs to be updated for Beta2. This post talks about how to-do this (it is the last paragraph of the post): http://weblogs.asp.net/scottgu/archive/2006/11/08/ASP.NET-AJAX-1.0-Beta-2-Release.aspx

    Hope this helps,

    Scott

  • Hi Scott,
    After downgrading to Beta 1, I was still getting same error in Test site. I added this bit and that seems to have solved, at least on Beta 1. I am not going to upgrade to Beta 2 yet to see if it solved that also.








  • I'm running into the same issue with the 'Sys' is undefined error since upgrading to BETA 2. My site worked great in BETA 1 - did I make a mistake in trying to upgrade?

    It seemed related to the use of ComponentArt's Web.UI controls, but after upgrading it seemed to alleviate the problem. But alas, they're back!

    I've added the ScritResource.axd HttpHandler and I still get the issue, but not on every page.

    Please help!

  • Hi Michael,

    Can you send me details about the issue you are running into and attach your web.config file in an email? I'll then have someone on the team investigate and help.

    Thanks,

    Scott

  • I too am getting this same sys is undefined issue, I was curious to find out whether others who have recieved this problem were using forms authentication as it only seems to have shown up after I started to use forms authentication, it usually happens after some kind of async postback has happened.. btw I also corrected the web config when upgrading to beta 2 and was recieving no errors until i changed over to forms authentication

    kind regards,

  • Just to follow up, I tracked my error to be inside the global.asax inside Protected Sub Application_AcquireRequestState(ByVal sender As Object, ByVal e As EventArgs), why it was causing it to have that sys undefined error i don't know but fix the problem inside there seems to have rectified my problem, thanks anyway

  • With forms authentication the block mentioned by Sam needs to be added to web.config in addition to to resolve the 'Sys' is undefined error in Beta 2.

  • Hi Scott,

    I have Formview and UpdatePanel inmy aspx page and would like to take advantage of AJAX technology, but I get this error.

    Parser Error
    Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

    Parser Error Message: Type 'Microsoft.Web.UI.UpdatePanel' does not have a public property named 'FormView'.

    Source Error:


    Line 23:
    Line 24:
    Line 25:
    Line 26:
    Line 27:

    Thank you.

  • Hi Volkan,

    The problem you have above is that you aren't nesting the control within a . You need to have this within updatepanel before you declare controls within it.

    Hope this helps,

    Scott

  • Hi Sean,

    Any chance you could send me an email describing this issue more (scottgu@microsoft.com)?

    I'll then loop-in someone from my team who can help investigate and get back to you.

    Thanks,

    Scott

  • I had the same sys undefined error in my test environment (development worked fine). I solved it by copying the Microsoft.Web.Preview.dll in to the bin-folder of my web. I think the handler for scriptResource.axd, is not able to extract the embedded resources out of Microsoft.Web.Preview.dll, if it resides in the GAC. Hope, that helps.

    Stefan

  • Hi Scott,

    I emailed you in regards to my previous problem, trying to reproduce the problem in a test project is a work in progress as I said in the email, but I had an additional query, I wasn't sure whether to hassle you about it via email though, so I thought I'd ask here, I'm not sure whether it relates to the update panel, but I'm sure it relates to asp.net ajax, while making my test project I noticed the size of the page using asp.net ajax, I have this test project and it has a control with a panel, a textbox, a label and 2 linkbuttons in it, I have a master page with a link, and a default page with 2 panels and a link, the transferred page size is 408kb, does that sound right to you, as to me that size seems waaay to large for what it is.

  • Hi Sean,

    That sounds pretty odd - you definitely shouldn't be seeing a download size that big.

    Two questions:

    1) Can you check your web.config file to make sure that you have debug="false" set? It could be that we are outputting the debug JavaScript scripts which are big (since they include extra assert/validation logic).

    2) If you still see this size feel free to send me a sample that repros it.

    Thanks,

    Scott

  • Scott,
    To follow up on my last problem, I have managed to work around the issue by referencing the scriptmanager and directly registering the lbUpload button as a postback control:
    &nbsp; &nbsp;Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    &nbsp; &nbsp; &nbsp; &nbsp;&#39;force the lbUpload button to cause a postback even though it is contained in an updatepanel..
    &nbsp; &nbsp; &nbsp; &nbsp;sm = CType(Parent.FindControl(&quot;MasterScriptManager&quot;), ScriptManager)
    &nbsp; &nbsp; &nbsp; &nbsp;sm.RegisterPostBackControl(lbUpload)
    &nbsp; &nbsp;End Sub
    Thanks :)
    kind regards,
    Sean.

  • Hi All,

    I have used Atlas when i am click the button.

    I have found error like "Unknow error"

    Plz help me.

    Lalit

  • Hi,
    We have aspx pages with ajax beta updatepanel and scriptmanager.
    When session ends it shows message sys.webform.pagerequestparsererror excepition and do nothing after that.How to catch this exception and redirect page to login again.
    We used
    protected void scriptManager1_AsyncPostBackError but did not work.

  • None of this stuff worked for me but the below did.. I know it's wrong, but I did it anyway, accessing private properties. I hard coded this to prove the concept, you'll need to find the control for each one and get its clientid and uniqueid and add it to a string and spit out the below statement to make any control ont he page no matter how nested, master pages, etc. cause a postback. Sure woul dbe nice if there was a javascript function we could attach to a button and call Set_isPostback() or something.

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm._postBackControlIDs = new Array("ctl00$AdminContent$GridView1$ctl06$AddButton");
    prm._postBackControlClientIDs = new Array("ctl00_AdminContent_GridView1_ctl06_AddButton");

  • I have AJAX ASP.NET RC1 installed and I am getting the 'Sys' is undefined error. Surpriseingly AJAX controltoolkit samples work great but when I try to use UpdatePanel in my project and put datagrid in it, I am getting this error on clicking Edit or jut visitng this page.
    Did you or your team found any resolution?

    Thanks

  • Just another hint; I had the following in my Global.asax:

    Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
    Dim P As Page = HttpContext.Current.Handler
    End Sub

    And that caused a 'Sys is undefined' to occur; commented out the 'Dim P As Page = HttpContext.Current.Handler' statement and the error went away (btw using RC1 AJAX)

    Regards,
    Soren, Denmark

Comments have been disabled for this content.