This thing is drive me crazy. I’m trying to intercept an exception server-side, during a partial postback, setting an error message to show on the client in a javascript alert.
But I just receive the following error on the firefox console:
uncaught exception: [Exception... "'Sys.WebForms.PageRequestManagerServerErrorException: Sys.WebForms.PageRequestManagerServerErrorException: Tentativo di divisione per zero.' when calling method: [nsIDOMEventListener::handleEvent]" nsresult: "0x8057001c (NS_ERROR_XPC_JS_THREW_JS_OBJECT)" location: "JS frame :: resource://firebug_rjs/net/spy.js :: callPageHandler :: line 796" data: no]
and no alert at all.
I got this problem on a complex project that involves Telerik components, but I’ve reduced the problem to its fundamentals, following this simple example, and the problem still exists.
The case:
Default.aspx
<form id="form1" runat="server">
<asp:ScriptManager ID="scriptManager" runat="server" OnAsyncPostBackError="scriptManager_OnAsyncPostBackError"></asp:ScriptManager>
<div>
<asp:UpdatePanel ID="upd" runat="server">
<ContentTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:Button ID="btnSend" runat="server" Text="Send" OnClick="btnSend_OnClick" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</form>
Default.aspx.cs
protected void scriptManager_OnAsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
scriptManager.AsyncPostBackErrorMessage = e.Exception.Message;
}
protected void btnSend_OnClick(object sender, EventArgs e)
{
// this will throw and Exception
int aa = Convert.ToInt32(TextBox1.Text) / Convert.ToInt32(TextBox2.Text);
}
I’ve tried with VS2010/.NET 4.0 and with VS2008/.NET 3.5, in Firefox and Internet Explorer, the problem is the same.
Please, anyone has some idea?
In addition to handling logic on server side you have to add script in order to handle error on client side.
This script is handling end_Request event, and if there was some error fires alert.