在捕获用户控件ASP.NET未处理的异常 [英] Catching unhandled exceptions in ASP.NET UserControls

查看:138
本文介绍了在捕获用户控件ASP.NET未处理的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态加载用户控件将它们添加到Web表单的Controls集合。

I'm dynamically loading user controls adding them to the Controls collection of the web form.

我想隐藏的用户控制,如果他们导致未处理的异常,而渲染。

I'd like to hide user controls if they cause a unhandled exception while rendering.

所以,我试图挂钩到每个用户控件的错误事件,但似乎这一事件永远不会触发了因为它为Page类的用户控件。

So, I tried hooking to the Error event of each UserControl but it seems that this event never fires for the UserControls as it does for Page class.

做了一些谷歌上搜索周围,它似乎并不乐观。这里任何想法?

Did some googling around and it doesn't seem promising. Any ideas here?

推荐答案

mmilic,从下面就<一个href=\"http://stackoverflow.com/questions/10793/catching-unhandled-exceptions-in-aspnet-usercontrols#10910\"相对=nofollow>您回应以我的<一个href=\"http://stackoverflow.com/questions/10793/catching-unhandled-exceptions-in-aspnet-usercontrols#10815\"相对=nofollow> previous想法 ..

mmilic, following on from your response to my previous idea..

不需要额外的逻辑!这一点,你做的问题没什么好类,正好包裹他们在某些实例泡沫包装! :)

No additional logic required! That's the point, your doing nothing to the classes in question, just wrapping them in some instantiation bubble-wrap! :)

OK,我只打算小点,但我希望看到这项工作为我自己,所以我拼凑起来的一些的非常的粗糙code,但这个概念是存在的,它似乎工作

OK, I was going to just bullet point but I wanted to see this work for myself, so I cobbled together some very rough code but the concept is there and it seems to work.

道歉为长POST

这基本上会是泡沫我提到..这将得到控制HTML,在渲染过程中捕获发生的所有错误。

This will basically be the "bubble" I mentioned.. It will get the controls HTML, catching any errors that occur during Rendering.

public class SafeLoader
{
    public static string LoadControl(Control ctl)
    {
        // In terms of what we could do here, its down
        // to you, I will just return some basic HTML saying
        // I screwed up.
        try
        {
            // Get the Controls HTML (which may throw)
            // And store it in our own writer away from the
            // actual Live page.
            StringWriter writer = new StringWriter();
            HtmlTextWriter htmlWriter = new HtmlTextWriter(writer);
            ctl.RenderControl(htmlWriter);

            return writer.GetStringBuilder().ToString();
        }
        catch (Exception)
        {
            string ctlType = ctl.GetType().Name;
            return "<span style=\"color: red; font-weight:bold; font-size: smaller;\">" + 
                "Rob + Controls = FAIL (" + 
                ctlType + " rendering failed) Sad face :(</span>";
        }
    }
}

以及一些控制..

好吧,我只是嘲笑聚集在这里两个控件,一是将引发其他将呈现垃圾。这里要说的,我不给废话。这些将与您的自定义的控件来代替。

And Some Controls..

Ok I just mocked together two controls here, one will throw the other will render junk. Point here, I don't give a crap. These will be replaced with your custom controls..

public class BadControl : WebControl
{
    protected override void Render(HtmlTextWriter writer)
    {
        throw new ApplicationException("Rob can't program controls");
    }
}

GoodControl

public class GoodControl : WebControl
{
    protected override void Render(HtmlTextWriter writer)
    {
        writer.Write("<b>Holy crap this control works</b>");
    }
}

好了,让我们看看测试页面。下面我简单的实例化控制,抓住他们的HTML输出它,我会按照与设计师的支持的想法等。

The Page

OK, so lets look at the "test" page.. Here I simply instantiate the controls, grab their html and output it, I will follow with thoughts on designer support etc..

    protected void Page_Load(object sender, EventArgs e)
    {
        // Create some controls (BadControl will throw)
        string goodHtml = SafeLoader.LoadControl(new BadControl());
        Response.Write(goodHtml);

        string badHtml = SafeLoader.LoadControl(new GoodControl());
        Response.Write(badHtml);
    }

的思考

好吧,我知道你在想什么,这些控件编程实例化,怎么样设计器支持?我花了该死的时间让这些控制很好的设计师,现在你用我的魔力搞乱。

Thoughts

OK, I know what you are thinking, "these controls are instantiated programatically, what about designer support? I spent freaking hours getting these controls nice for the designer, now you're messing with my mojo".

好了,我还没有真正测试过这个,但(可能会在一分钟呢!),但这里的想法是重写页面的CreateChildControls方法,并采取每个控件实例的窗体上添加并运行它通过该SafeLoader。如果code通行证,可以将其添加到Controls集合正常,如果没有,那么你可以创建错误的文字或东西,给你我的朋友。

OK, so I havent really tested this yet (probably will do in a min!) but the idea here is to override the CreateChildControls method for the page, and take the instance of each control added on the form and run it through the SafeLoader. If the code passes, you can add it to the Controls collection as normal, if not, then you can create erroneous literals or something, up to you my friend.

再次遗憾的长期职位,但我想在这里拿到code,所以我们可以讨论这个:)
我希望这有助于证明我的想法:)

Again, sorry for the long post, but I wanted to get the code here so we can discuss this :) I hope this helps demonstrate my idea :)

,工作正常,可能需要一些清理,以使事情变得更好看,但我会留给你;)

Tested by chucking a control in on the designer and overriding the CreateChildControls method with this, works fine, may need some clean up to make things better looking, but I'll leave that to you ;)

protected override void CreateChildControls()
{
    // Pass each control through the Loader to check
    // its not lame
    foreach (Control ctl in Controls)
    {
        string s = SafeLoader.LoadControl(ctl);
        // If its bad, smack it downnnn!
        if (s == string.Empty)
        {
            ctl.Visible = false; // Prevent Rendering
            string ctlType = ctl.GetType().Name;
            Response.Write("<b>Problem Occurred Rendering " + 
                ctlType + " '" + ctl.ID + "'.</b>");
        }
    }
}

享受!

这篇关于在捕获用户控件ASP.NET未处理的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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