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 个母版页:
- 一个是为了使页面的外观相似 (_BASE_MASTER),
- 一个是为了在网站的各个小节中进行一些更改(例如标题)(_BASE_SECTEUR_BASE)。
我还检查了两次、三次检查了页面之间的链接。如果没有“AddError”方法调用,一切都会正常工作。
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. 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) :
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想调用母版页内的方法,可以执行以下操作。
You can do the following if you want to call a method inside master page.
我终于找到了解决方法。这是我个人的解决方案。
现在我只需要打电话
ErrorHandler.AddPageError("", "");
从任何地方调用我的错误。
I finally found a workaround. Here's my personnal solution.
Now I only have to call
ErrorHandler.AddPageError("", "");
from anywhere to call my errors.