[英]Master's master's methods in ASP.NET C#
I am trying to do an error handling method for our company's intranet applications.我正在尝试为我们公司的内网应用程序做一个错误处理方法。 The error is shown in the page with a asp:label control.
错误显示在带有 asp:label 控件的页面中。 When I did inline coding, it was fine, but when I try putting the code in a method on a master page, it doesn't work.
当我进行内联编码时,这很好,但是当我尝试将代码放在母版页上的方法中时,它不起作用。 I get a compilation error.
我得到一个编译错误。 Here's the method (in the master.cs file):
这是方法(在 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));
}
}
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. '_BASE_MASTER' 在这种情况下不存在,尽管一切似乎都很好。 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:我使用 2 个母版页:
I also checked, double-checked, triple-checked for the links between the pages.我还检查、仔细检查、三重检查页面之间的链接。 Everything works just fine without the 'AddError' method call.
没有“AddError”方法调用,一切正常。
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("", "");
现在我只需要调用
ErrorHandler.AddPageError("", "");
from anywhere to call my errors.从任何地方调用我的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.