[英]Master's master's methods in ASP.NET C#
我正在嘗試為我們公司的內網應用程序做一個錯誤處理方法。 錯誤顯示在帶有 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 個母版頁:
我還檢查、仔細檢查、三重檢查頁面之間的鏈接。 沒有“AddError”方法調用,一切正常。
如果要在母版頁中調用方法,可以執行以下操作。
// 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");
}
}
我終於找到了解決方法。 這是我的個人解決方案。
/// <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("", "");
從任何地方調用我的錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.