Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 34455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T14:04:52+00:00 2026-05-10T14:04:52+00:00

I’m dynamically loading user controls adding them to the Controls collection of the web

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T14:04:53+00:00Added an answer on May 10, 2026 at 2:04 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.