Master's master's methods in ASP.NET C#
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));
}
}
No troubles yet... It works if I am in the first content page :
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");
}
}
It is wierd, it gives a small error, but it works (I wouldn't normaly use this in a load event).
It's in the second page that it doesn't work.
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' doesn't exists in this context, although everything seems ok. I've been trying for a couple hours now and I can't seem to find a solution. Maybe someone could help?
A couple more precisions :
I use 2 master pages :
- One to make the look of pages similar (_BASE_MASTER),
- One to make some changes (like the title) in subsections of the site(_BASE_SECTEUR_BASE).
I also checked, double-checked, triple-checked for the links between the pages. Everything works just fine without the 'AddError' method call.
You can do the following if you want to call a method inside master page.
// 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");
}
}
I finally found a workaround. Here's my personnal solution.
/// <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));
}
}
Now I only have to call
ErrorHandler.AddPageError("", "");
from anywhere to call my errors.
精彩评论