I have a message zone defined in the master page (an update panel with a label inside). I want to handle all the exceptions globally and I thought I can catch them in the Application_Error event in Global.asax and from there change the label text and update the panel. But the text doesn't refresh, probably because I don't understand correctly the page life cycle. Is it possible to change a label text in the application_error event? This is my code:
void DisplayErrorOnPage(AppException myEx)
{
System.Web.UI.Page page = System.Web.HttpContext.Current.Handler
as System.Web.UI.Page;
if (page != null)
{
Label ErrorLabel = page.Master.Master.FindControl("StatusMessage")
as Label;
ErrorLabel.Text = myEx.Message;
UpdatePanel up = page.Master.Master.FindControl("StatusMessagePanel")
as UpdatePanel;
up.Update();
}
}
void Application_Error(object sender, EventArgs e)
{
Exception myEx = Server.GetLastError().GetBaseException();
//if it's a controlled exception, display it on the current page
//otherwise redirect to an error page
if (myEx is AppException)
{
if (((AppException)myEx).RedirectToErrorPage)
RedirectToErrorPage(myEx);
else
DisplayErrorOnPage((AppException)myEx);
}
else
{
RedirectToErrorPage(myEx);
}
}
In the Site.Master the message zone is defined like this:
<asp:UpdatePanel runat="server" UpdateMode="Conditional" ID="StatusMessagePanel">
<ContentTemplate>
<asp:Label runat="server" ID="StatusMessage" Text="Message zone" />
</ContentTemplate>
</asp:UpdatePanel>
The thing is possible ,And why the text does not refresh ? There could be something wrong with your ajax implementation and you will get better answers if you add your page code ! but do you know that Application_Error is an application level state management mechanism ? So be carefull to use Application_Error event to catch application level errors and not user level errors !