我该如何处理我所有的错误/信息在一个地方一个Asp.Net页面上? [英] How can I handle all my errors/messages in one place on an Asp.Net page?

查看:143
本文介绍了我该如何处理我所有的错误/信息在一个地方一个Asp.Net页面上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一些指导这里。

I'm looking for some guidance here.

在我的网站,我把Web用户控件的东西。例如,我将有一个NewsItem控制,一个控制条,ContactForm的一个控制。

On my site I put things in Web user controls. For example, I will have a NewsItem Control, an Article Control, a ContactForm control.

这会出现在我的网站上不同的地方。

These will appear in various places on my site.

我不想紧密结合他们,所以我想我会与活动/代表做到这一点。我有点不清楚的我将如何实现这一点,虽然。

I don't want to tightly couple them, so I think I will have to do this with Events/Delegates. I'm a little unclear as to how I would implement this, though.

一个几个例子:

一个接触的形式提交。它的提交,而不是用自身替换您的邮件已发送这限制了信息的位置后,我想只是通知控制是一个状态消息,或许建议的行为页面。因此,消息包括文本渲染以及一个枚举 DisplayAs.Popup DisplayAs.Success

A contact form is submitted. After it's submitted, instead of replacing itself with a "Your mail has been sent" which limits the placement of that message, I'd like to just notify the page that the control is on with a Status message and perhaps a suggested behaviour. So, a message would include the text to render as well as an enum like DisplayAs.Popup or DisplayAs.Success

这是条控制查询文章对象的数据库。数据库返回异常。自定义的异常传递给页面与 DisplayAs.Error 枚举一起。页面处理此错误,并显示它无论错误去了。

An Article Control queries the database for an Article object. Database returns an Exception. Custom Exception is passed to the page along with the DisplayAs.Error enum. The page handles this error and displays it wherever the errors go.

我试图完成类似的ValidationSummary控制的东西,但我希望页面能够作为枚举觉得适合显示的消息。

I'm trying to accomplish something similar to the ValidationSummary Control, except that I want the page to be able to display the messages as the enum feels fit.

同样,我不想裹紧还是依靠现有页面上的控制。我想要的控件引发这些事件,但如果它想要的页面可以忽略它们。

Again, I don't want to tightly bind or rely a control existing on the Page. I want the controls to raise these events, but the page can ignore them if it wants.

我要对这个正确的方式?

Am I going about this the right way?

我喜欢一个 code 样品只是为了让我开始。

I'd love a code sample just to get me started.

我知道这是一个更复杂的问题,所以我会投票前/等待更长的时间选择的答案。

推荐答案

您可以泡了事件中的孩子发生父页面。父页面可以注册该事件,并利用它(如果它是很有用的)。

You can bubble up the event occurs in child to parent page. Parent page can register that event and utilizes it (if it is useful).

父ASPX

<uc1:ChildControl runat="server" ID="cc1" OnSomeEvent="cc1_SomeEvent" />

母公司C#

protected void cc1_SomeEvent(object sender, EventArgs e)
{
    // Handler event
}

儿童C#

public event EventHandler OnSomeEvent;

protected void ErrorOccurInControl()
{
     if (this.OnSomeEvent != null)
     {
          this.OnSomeEvent(this, new EventArgs());
     }
}

protected override void OnLoad(EventArgs e)
{
     ErrorOccurInControl();
}

这篇关于我该如何处理我所有的错误/信息在一个地方一个Asp.Net页面上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆