マスター ページ (ラベルが内側にある更新パネル) で定義されたメッセージ ゾーンがあります。すべての例外をグローバルに処理したいので、Global.asax の Application_Error イベントでそれらをキャッチし、そこからラベル テキストを変更してパネルを更新できると考えました。しかし、おそらくページのライフサイクルを正しく理解していないため、テキストは更新されません。application_error イベントでラベル テキストを変更することはできますか? これは私のコードです:
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);
}
}
Site.Master では、メッセージ ゾーンは次のように定義されます。
<asp:UpdatePanel runat="server" UpdateMode="Conditional" ID="StatusMessagePanel">
<ContentTemplate>
<asp:Label runat="server" ID="StatusMessage" Text="Message zone" />
</ContentTemplate>
</asp:UpdatePanel>