mmilic, following from your answer to the previous idea.
No additional logic required! This is that you are not doing anything with the classes in question, just wrapping them in some kind of instantiation bubble! :)
OK, I was going to just point out the point, but I wanted to see this work for myself, so I put together very crude code, but the concept is there and it seems to work.
APOLOGIES FOR LONG MAIL
Safeloader
Basically it will be the βbubbleβ that I mentioned. It will receive HTML controls, catching any errors that occur during rendering.
public class SafeLoader { public static string LoadControl(Control ctl) {
And some controls ..
Well, I just scoffed at two control elements here, one will throw another, there will be garbage. Here, I don't hold shit. They will be replaced by your user 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>"); } }
Page
OK, so let's look at the test page. Here I just create controls, grab their html and display it, I will follow the thoughts on supporting designers, etc.
Page Code Behind
protected void Page_Load(object sender, EventArgs e) {
Thoughts
OK, I know what you think, "these controls are programmed, and as for the support of designers? I spent many hours making these controls pleasant for the designer, now you mess with my mojo."
OK, so I really havenβt tested it yet (maybe I'll do it in a minute!), But the idea here is to override the CreateChildControls method for the page and take an instance of each control added to the form and run it SafeLoader. If the code passes, you can add it to the Controls collection as usual, if not, then you can create erroneous literals or something else, up to your friend.
Finally..
Again, sorry for the long post, but I wanted to get the code here so we can discuss it :) Hope this helps demonstrate my idea :)
Update
Tested by inserting the control into the constructor and overriding the CreateChildControls method with this, it works fine, it may take some cleanup to improve the situation, but I'll leave it 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 it!