在Global.asax中更改标签文本

问题描述:

我在主页面(带有内部标签的更新面板)中定义了一个消息区域。我想处理全局的所有异常,我想我可以在Global.asax中的Application_Error事件中捕获它们,并从那里更改标签文本并更新面板。但文字不刷新,可能是因为我不正确理解页面生命周期。是否可以更改application_error事件中的标签文本?这是我的代码:在Global.asax中更改标签文本

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> 

的事情是可能的,为什么文本不会刷新?你的ajax实现可能有问题,如果你添加页面代码,你会得到更好的答案!但是你知道Application_Error是一个应用程序级别的状态管理机制吗?因此请小心使用Application_Error事件来捕获应用程序级错误而不是用户级错误!

+0

我已经添加了Site.Master页面 – 2012-08-08 12:42:28

+0

中的相关部分现在我想到了,我应该更改Page_Error中的标签文本而不是Application_Error? – 2012-08-08 13:15:43