views:

520

answers:

4

I've made a web application that has the following architecture: Every form is a UserControl, there is just one actual page (Default.aspx), and a parameter in the URL specifies which UserControl to load. The UserControl is loaded in an UpdatePanel so that it can enjoy full AJAX-iness.

There is also an elaborate message displaying mechanism that I use. Eventually messages end up in one designated area on top of the Default.ASPX with some nice formatting 'n stuff.

Now, I would also like to capture any unhandled exceptions that originate in the UserControl and display it in this area with all the bells-and-whistles that I've made for messages.

How can I do this? The Page.Error and ScriptManager.AsyncPostBackError somehow don't work for me...

A: 

I'm not sure if you meant Page.Error on your main page wasn't working or on the user controls themselves. If you haven't done so already, you could try creating the following class:

public class CustomUserControl : UserControl
{
   public new CustomUserControl
   {
      this.Error += new EventHandler(Page_Error);
   }

   protected void Page_Error(object source, EventArgs e)
   {
      Exception exception = HttpContext.Current.Server.GetLastError();
      // Message away
   }
}

Then change all your user controls to inherit this class instead.

Spencer Ruport
It's an idea. But perhaps I can do this somehow on the Default.aspx level?
Vilx-
I dunno about the whole bit but you could change Page_Error to be public in your _Default class and change the constructor for CustomUserControl to this.Error += new EventHandler(((_Default)this.Page).Page_Error);
Spencer Ruport
The truth is - I wanted to avoid having my usercontrols inherit from a specific base class. It's possible, but I'd rather not. You see, they come from plugin assemblies, and I expect there to be a lot of those in the future. I try to put as little requirements to them as possible.
Vilx-
Sorry about the delay there. Could you post a bit ofthe code where you are dynamically loading these controls? I assume that's what you're doing. If so, the Error event is public so you can add the EventHandler from within the _Default page.
Spencer Ruport
A: 

Can you give some more detail of what you've tried with ScriptManager.AsyncPostBackError? That really should be the right thing to use.

teedyay
A: 

The full answer is in my blog post - Unhide Exceptions Hidden By AJAX.

I do use AsyncPostBackError() to change the error message from the generic "Exception has been thrown by the target of an invocation" to the real error message. I set a label to the error text and then I tell AJAX that I handled the error with

args.set_errorHandled(true);
JBrooks
A: 

For unhandled errors look at the Global.asax file...

protected void Application_Error(object sender, EventArgs e)
{

    //  fired when an unhandled error occurs within the application.
    //  ..or use an HttpModule instead
}

or create a custom HttpModule instead that you plug-in your web.config...

public class UnhandledExceptionModule : IHttpModule
{
    #region IHttpModule Members
    public void Dispose()
    {
    }

    public void Init(HttpApplication app)
    {           
        app.Error += new EventHandler(app_Error);
    }

    protected void app_Error(object sender, EventArgs e)
    {
        HttpApplication app = sender as HttpApplication;
                Exception ex =  app.Server.GetLastError().GetBaseException();
Konrad