1

マスター ページ (ラベルが内側にある更新パネル) で定義されたメッセージ ゾーンがあります。すべての例外をグローバルに処理したいので、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>
4

1 に答える 1

1

それは可能です、そしてなぜテキストが更新されないのですか?ajaxの実装に問題がある可能性があり、ページコードを追加すると、より良い回答が得られます。しかし、Application_Errorがアプリケーションレベルの状態管理メカニズムであることをご存知ですか?したがって、Application_Errorイベントを使用して、ユーザーレベルのエラーではなく、アプリケーションレベルのエラーをキャッチするように注意してください。

于 2012-08-08T11:25:20.083 に答える