Capturing exceptions from a Page in a usercontrol
Can a user control capture the Page.Error event of it's 开发者_JAVA技巧parent page and log that exception?
You can do it, but it is not the best solution it only works if the error occurs after the event has been registered, which means after the control loads, since the page loads first it won't catch any errors in the page load. A better solution would be to use the Application_Error event in the global.asax.
Put this code in your control:
protected void Page_Load(object sender, EventArgs e)
{
Page.Error += new EventHandler(Page_Error);
}
void Page_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
Response.Write(exception.ToString());
HttpContext.Current.ClearError();
}
精彩评论