UpdatePanel alert not showing when server exception thrown

Last post 02-09-2009 3:01 PM by dezza. 4 replies.

Sort Posts:

  • UpdatePanel alert not showing when server exception thrown

    02-09-2009, 12:17 AM
    • Member
      point Member
    • dezza
    • Member since 02-08-2009, 10:04 PM
    • Posts 3

    XP 32bit
    Visual Studio 2008
    .NET 3.5 SP1
    IE 7.0
    I have not installed the AJAX toolkit from this site - I am using what came 'out of the box' with Visual Studio 2008.

    I am attempting a simple test with the UpdatePanel. I have set up the example outlined here:

    http://msdn.microsoft.com/en-us/library/bb398934.aspx

    However, when I cause an exception to be thrown on the server (by performing a calculation like 12/0), the alert box is not shown. Instead the text 'Error on page.' is shown on the status bar in IE on the left and clicking this gives the details of the error as:

    Line: 4724
    Char: 21
    Error: Sys.WebForms.PageRequestManagerServerErrorException: Attempted to divide by zero. You can't divide 12 by 0.
    Code: 0
    Url: http://localhost/test/Default.aspx

    I've published the site to IIS and tried and this makes no difference.

    I am expecting an alert box to be shown by the client browser when the exception is detected by the AJAX code on the client side.

    Any help would be much appreciated. Have reproduced the code I am running below (from the link above):

     

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                int a = Int32.Parse(TextBox1.Text);
                int b = Int32.Parse(TextBox2.Text);
                int res = a / b;
                Label1.Text = res.ToString();
            }
            catch (Exception ex)
            {
                if (TextBox1.Text.Length > 0 && TextBox2.Text.Length > 0)
                {
                    ex.Data["ExtraInfo"] = " You can't divide " +
                        TextBox1.Text + " by " + TextBox2.Text + ".";
                }
                throw ex;
            }       
        }
        protected void ScriptManager1_AsyncPostBackError(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.";
            }
        }
    </script>

    <html >
    <head id="Head1" runat="server">
        <title>Partial-Page Update Error Handling Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <asp:ScriptManager ID="ScriptManager1" runat="server" OnAsyncPostBackError="ScriptManager1_AsyncPostBackError">
                </asp:ScriptManager>
                <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                    <ContentTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Width="39px"></asp:TextBox>
                        /
                        <asp:TextBox ID="TextBox2" runat="server" Width="39px"></asp:TextBox>
                        =
                        <asp:Label ID="Label1" runat="server"></asp:Label><br />
                        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="calculate" />
                    </ContentTemplate>
                </asp:UpdatePanel>
            </div>
        </form>
    </body>
    </html>
     

  • Re: UpdatePanel alert not showing when server exception thrown

    02-09-2009, 1:28 AM
    • Participant
      778 point Participant
    • amisol
    • Member since 10-23-2007, 3:19 AM
    • Scottsadle
    • Posts 150

     It basically means that your JavaScript has a code that throws a division by zero error.

    You should change your browser settings to allow script  debugging (ie options -> Advanced -> uncheck disable script debugging)

    Then you will be able to step into the error and debug it.

    Please mark post as answer if I helped you
  • Re: UpdatePanel alert not showing when server exception thrown

    02-09-2009, 1:34 AM
    • Member
      point Member
    • dezza
    • Member since 02-08-2009, 10:04 PM
    • Posts 3

     Thanks for the response.

     The idea behind the example is to show that an alert() is shown by the update panel when a server side exception is thrown. Thus I am expecting an exception to be thrown when I enter 12/0, but I am also expecting the update panel to show an alert() which it does not. I'm trying to work out why the alert() is not shown.

     Thanks

  • Re: UpdatePanel alert not showing when server exception thrown

    02-09-2009, 2:37 PM
    Answer
    • Participant
      778 point Participant
    • amisol
    • Member since 10-23-2007, 3:19 AM
    • Scottsadle
    • Posts 150

     I would guess that int res = a / b; is the problem. You should read more carefully how exceptions work in JavaScript as I'm not sure you can do what you're trying to.

    Please mark post as answer if I helped you
  • Re: UpdatePanel alert not showing when server exception thrown

    02-09-2009, 3:01 PM
    • Member
      point Member
    • dezza
    • Member since 02-08-2009, 10:04 PM
    • Posts 3

     Hi there,

     Thanks for the response.

     I realise that int res = a/b will throw the exception, but this is the point of this example. I've taken the code 'as is' from the example Microsoft have on their site called 'Customizing Error Handling for ASP.NET UpdatePanel Controls' which can be seen here 'http://msdn.microsoft.com/en-us/library/bb398934.aspx'.

    In their example, they say at step10 an alert box should be shown with the message. However, I don't get this - I get a error shown in the status bar 'Error on page' and the alert() is not shown. I'm trying to work out why the alert is not shown.

     Thanks for your help with this.

     

Page 1 of 1 (5 items)