【问题标题】:Master's master's methods in ASP.NET C#ASP.NET C#中高手的高手方法
【发布时间】:2011-07-12 16:27:48
【问题描述】:


我正在尝试为我们公司的 Intranet 应用程序做一个错误处理方法。错误显示在带有 asp:label 控件的页面中。当我进行内联编码时,这很好,但是当我尝试将代码放在母版页上的方法中时,它不起作用。我得到一个编译错误。这是方法(在 master.cs 文件中):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _BASE_MASTER : System.Web.UI.MasterPage
{
    public void AddError(string strWhen, string strMessage)
    {
        lblAlert.Text += "<p>" + "Une erreur s'est produite " + strWhen + "<br/>'" + strMessage + "'</p>";
        lblAlert.RenderControl(new HtmlTextWriter(Response.Output));
    }
}

还没有问题...如果我在第一个内容页面,它可以工作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _BASE_SECTEUR_BASE : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ((_BASE_MASTER)Master).AddError("TEST", "TEST2");
    }
}

这很奇怪,它给出了一个小错误,但它可以工作(我通常不会在加载事件中使用它)。

它在第二页不起作用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class _BASE_SECTEUR_Default : System.Web.UI.Page
{
    bool errorOccured = false;
    [...]
        }
        catch (Exception e)
        {
            if (!errorOccured)
            {
                ((_BASE_MASTER)((_BASE_SECTEUR_BASE)Master).Master).AddError("lors de l'acquisition du code congé.", e.Message);
                errorOccured = true;
            }
        }
    [...]
}

'_BASE_MASTER' 在这种情况下不存在,尽管一切看起来都很好。我已经尝试了几个小时,但似乎找不到解决方案。也许有人可以帮忙?

更多的精度:

我使用 2 个母版页:

  • 使页面外观相似的一个 (_BASE_MASTER),
  • 在网站的子部分 (_BASE_SECTEUR_BASE) 中进行一些更改(如标题)。

我还检查、反复检查、反复检查页面之间的链接。没有 'AddError' 方法调用一切正常。

【问题讨论】:

    标签: c# asp.net error-handling master-pages


    【解决方案1】:

    如果要在母版页中调用方法,可以执行以下操作。

    // Level0 Master Page
    public partial class Root : System.Web.UI.MasterPage
    {
        public void AddError(string strWhen, string strMessage)
        {
            lblAlert.Text += "<p>" + "Une erreur s'est produite " + strWhen + "<br/>'" + strMessage + "'</p>";
        }
    }
    
    // Level1 Master Page
    public partial class OneColumn : System.Web.UI.MasterPage
    {
        public void AddError(string strWhen, string strMessage)
        {
            ((Root)Master).AddError(strWhen, strMessage);
        }
    }
    
    // Content Page
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ((OneColumn)Master).AddError("test", "test");
        }
    }
    

    【讨论】:

      【解决方案2】:

      我终于找到了解决方法。这是我的个人解决方案。

      /// <summary>
      /// Fonctions de gestion d'erreurs personnalisées.
      /// </summary>
      public class ErrorHandler
      {
          static string errLog = HttpContext.Current.Server.MapPath("~/Logs/errors.log");
      
          /// <summary>
          /// Affiche dans la page qu'une erreur s'est produite et l'indique dans un
          /// journal.
          /// Utiliser seulement si l'erreur est récupérable.
          /// </summary>
          /// <param name="strWhen">Complète la chaîne "Une erreur s'est produite ".
          /// Un "." sera ajouté après. Ex : "lors du chargement du calendrier"</param>
          /// <param name="strMessage">Le message d'erreur de l'exception. Sera encadré
          /// d'apostrophes.</param>
          static public void AddPageError(string strWhen, string strMessage)
          {
              string strPrefixe = "Une erreur s'est produite ";
              string strPage = HttpContext.Current.Request.Url.AbsolutePath;
              MasterPage mpMaster = ((Page)HttpContext.Current.Handler).Master;
      
              using (TextWriter errFile = new StreamWriter(errLog, true))
              {
                  errFile.WriteLine(DateTime.Now.ToString() + " - (" + strPage + ") - " + strPrefixe + strWhen + " : '" + strMessage + "'");
              }
      
              Label lblAlert = (Label)((Page)HttpContext.Current.Handler).FindControl("lblAlert");
      
              // La boucle suivante sert à remonter les master page pour vérifier si un Label avec un id lblAlert existe.
              while (lblAlert == null)
              {
                  if (mpMaster == null)
                      return;
      
                  lblAlert = (Label)mpMaster.FindControl("lblAlert");
                  mpMaster = mpMaster.Master;
              }
      
              // On ne veut pas continuer si le Label n'existe pas : Des erreurs se produiraient.
              if (lblAlert == null)
                  return;
      
              if (lblAlert.Text == "")
              {
                  lblAlert.Text = "<p><i>Cliquez pour faire disparaître.</i></p>";
              }
      
              lblAlert.Text += "<p>" + strPrefixe + strWhen + ".<br/>'" + strMessage + "'</p>";
              lblAlert.BorderWidth = Unit.Parse("0.3em");
              lblAlert.RenderControl(new HtmlTextWriter(HttpContext.Current.Response.Output));
          }
      }
      

      现在我只需要打电话 ErrorHandler.AddPageError("", ""); 从任何地方调用我的错误。

      【讨论】:

        猜你喜欢
        • 2021-12-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-14
        • 2011-12-23
        • 2012-02-21
        • 1970-01-01
        相关资源
        最近更新 更多