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 回答