views:

1189

answers:

10

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.

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?

A: 

Global.asax and Application_Error?

http://www.15seconds.com/issue/030102.htm

Or the Page_Error Event on an individual Page only:

http://support.microsoft.com/kb/306355

void Page_Load(object sender, System.EventArgs e)
{
    throw(new ArgumentNullException());
}

public void Page_Error(object sender,EventArgs e)
{
    Exception objErr = Server.GetLastError().GetBaseException();
    string err =    "<b>Error Caught in Page_Error event</b><hr><br>" + 
                    "<br><b>Error in: </b>" + Request.Url.ToString() +
                    "<br><b>Error Message: </b>" + objErr.Message.ToString()+
                    "<br><b>Stack Trace:</b><br>" + 
                      objErr.StackTrace.ToString();
    Response.Write(err.ToString());
    Server.ClearError();
}

Also, Karl Seguin (Hi Karl!) had a Post on using HttpHandler instead:

http://codebetter.com/blogs/karlseguin/archive/2006/06/12/146356.aspx

(Not sure what the permission to reproduce it, but if you want to write up an answer, you got my Upvote ☺)

Michael Stum
+2  A: 

Thanks for the help but that's not exactly what I was asking.

I can catch unhandled exceptions in the Error event of the page, but I can't get the reference to the UserControl that caused the exception so I could hide just that control.

Even if I could get that information somehow through the stack trace (which would be awful way to do it) the execution of the Page is halted and no subsequent controls would render.

Maybe someone knows how DotNetNuke handles that, because this is a similar solution...

muerte
A: 

How about adding a new sub-class of UserControl that error-handles its render and load methods (so that they hide as you wish) and then inheriting from that for your user controls?

Keith
+4  A: 

This is an interesting problem.. I am still pretty fresh when it comes to custom controls etc, but here are my thoughts (feel free to comment/correct people!).. (I am kinda thinking/writing out loud here!)

  • If an error occurs during rendering, in some cases, would it not be too late? (since some of the controls HTML may have already been sent to the Writer and output).
  • Therefore, would it not be best to wrap the user control's Render method, but rather than passing it the reference to the "Live" HtmlTextWriter, you pass your own, trap any Exceptions raised in this little safety "bubble", if all goes well, you then pass your resultant HTML to the actual HtmlTextWriter?
  • This logic could probably be slung to a generic wrapper class which you would use to dynamically load/render the controls at run time..
  • If any errors do occur, you have all the information you need at your disposal! (i.e control references etc).

Just my thoughts, flame away! :D ;)

Rob Cooper
A: 

@Keith Can you show me a simple code sample of your idea, i'm not sure I understood you completely? As I see it, if I inherit from the base class and override the usercontrols Page_Load event, I think my base class code wouldn't even fire...

@Rob Thanks for the comments. That's an interesting idea that might work, but it requires that each UserControl implements some additional logic, and it is not under my control. I will load customly developed user controls, which will implement some interfaces, but I can't control how they will render their output.

Bottom linem, my project is something similar to Dot Net Nuke project. I load controls which are developed by other people, and I would like to make sure that error in one control doesn't affect other controls or the entire page.

muerte
+1  A: 

Depending on where your errors are occurring you can do something like...

public abstract class SilentErrorControl : UserControl
{
    protected override void Render( HtmlTextWriter writer )
    {
        //call the base's render method, but with a try catch
        try { base.Render( writer ); }
        catch ( Exception ex ) { /*do nothing*/ }
    }
}

Then inherit SilentErrorControl instead of UserControl.

Keith
A: 

Keith, I understand now more clearly what you meant, thanks, but that solution still doesn't solve my problem.

It will catch only exceptions that are happening in the Render method.

What if an exception occurs in the PageLoad event of the user control? Or in some event handler from another control, a ButtonClick event might cause a exception...

muerte
+8  A: 

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, 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.

APOLOGIES FOR THE LONG POST

The SafeLoader

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..

BadControl

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>");
    }
}

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..

Page Code-Behind

    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".

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.

Finally..

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 :)

Update

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>");
        }
    }
}

Enjoy!

Rob Cooper
A: 

Rob, thanks a lot for the detailed solution, i've upmoded it, but it still doesn't solve my issue completely.

It will work if I catch all rendered html as a string, but unfortunately I have no control of that. I can't catch controls html and then render it myself. I can only load controls, add them to the Controls collections of another control and of they go.

And btw, sorry for the delay with my answer...

muerte
A: 

Hi mmilic, I am not sure I understand your response.. How are you loading your controls and adding them to your controls collection?

That was the whole point of the bit added in the "Update" section.. You have the flexibility to use the SafeLoader wherever you please.

I am not sure why you feel you don't have access/control over the Html? The goal of the SafeLoader is that you dont care what the html is, you simply try and "output" the control (within the "bubble") and determine if it loads OK in its current state.

If it does (i.e. the html is returned) then you can do what you like with it, output the html, add the control to the controls collection, whatever!

If not, then again, you can do what you like, render an error message, throw a custom exception.. The choice is yours!

I hope this helps clarify things for you, if not, then please shout :)

Rob Cooper