Reputation: 107062
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...
Upvotes: 2
Views: 1336
Reputation: 716
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();
Upvotes: 0
Reputation: 10013
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);
Upvotes: 1
Reputation: 23531
Can you give some more detail of what you've tried with ScriptManager.AsyncPostBackError? That really should be the right thing to use.
Upvotes: 0
Reputation: 35117
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.
Upvotes: 0