<h2>Summary</h2>
<p>Hi All,<br />
OK, further into my adventures with custom controls...</p>
<p>In summary, here is that I have learned of three main &quot;classes&quot; of custom controls. Please feel free to correct me if any of this is wrong!</p>
<ol>
<li><strong>UserControls</strong> - Which inherit from <em>UserControl</em> and are contained within an <em>ASCX</em> file. These are pretty limited in what they can do, but are a quick and light way to get some UI commonality with designer support.</li>
<li><strong>Custom Composite Controls</strong> - These are controls that inherit from <em>WebControl</em> where you add pre-existing controls to the control within the <em>CreateChildControls</em> method. This provides great flexibility, but lack of designer support without additional coding. They are highly portable though since they can be compiled into a DLL.</li>
<li><strong>Custom Rendered Controls</strong> - Similar to Custom Composite Controls, these are added to a Web Control Library project. The rendering of the control is completely controlled by the programmer by overriding the <em>Render</em> method.</li>
</ol>
<h2>My Thoughts..</h2>
<p>OK, so while playing with custom composites, I found the following:</p>
<ul>
<li>You have little/no control over the HTML output making it difficult to &quot;debug&quot;.</li>
<li>The <em>CreateChildControls</em> (and subsequent methods) can get real busy with <em>Controls.Add(myControl)</em> everywhere.</li>
<li>I found rendering tables (be it for layout or content) to be considerably awkward.</li>
</ul>
<h2>The Question(s)..</h2>
<p>So, I admit, I am new to this so I could be way off-base with some of my points noted above..</p>
<ul>
<li>Do you use Composites?</li>
<li>Do you have any neat tricks to control the HTML output?</li>
<li>Do you just say &quot;to hell with it&quot; and go ahead and create a custom rendered control?</li>
</ul>
<p><strong>Its something I am keen to get really firm in my mind since I know how much <em>good</em> control development can cut overall development time.</strong></p>
<p>I look forward to your answers ^_^</p>

[{'answer_id': 17542, 'author': 'Jon Limjap', 'author_id': 372, 'author_profile': 'https://Stackoverflow.com/users/372', 'pm_score': 4, 'selected': True, 'text': "<p>I say go ahead with the custom rendered control. I find that in most cases the composite can be easier done and used in a UserControl, but anything beyond that and you'd need to have a finer degree of control (pun unintended) to merit your own rendering strategy.</p>\n\n<p>There maybe controls that are simple enough to merit a composite (e.g., a textbox combined with a javascript/dhtml based datepicker, for example) but beyond that one example, it looks like custom rendered controls are the way to go.</p>\n"}, {'answer_id': 17546, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 1, 'selected': False, 'text': '<p>I often use composite controls. Instead of overriding Render or RenderContents, just assign each Control a CssClass and use stylesheets. For multiple Controls.Add, I use an extension method:</p>\n\n<pre><code>//Controls.Add(c1, c2, c3)\nstatic void Add(this ControlCollection coll, params Control[] controls)\n { foreach(Control control in controls) coll.Add(control);\n }\n</code></pre>\n\n<p>For quick and dirty rendering, I use something like this:</p>\n\n<pre><code>writer.Render(@"&lt;table&gt;\n                   &lt;tr&gt;&lt;td&gt;{0}&lt;/td&gt;&lt;/tr&gt;\n                   &lt;tr&gt;\n                       &lt;td&gt;", Text);\ncontrol1.RenderControl(writer);\nwriter.Render("&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;");\n</code></pre>\n\n<p>For initializing control properties, I use property initializer syntax:</p>\n\n<pre><code>childControl = new Control {  ID="Foo"\n                            , CssClass="class1"\n                            , CausesValidation=true;\n                           };\n</code></pre>\n'}, {'answer_id': 17710, 'author': 'Slavo', 'author_id': 1801, 'author_profile': 'https://Stackoverflow.com/users/1801', 'pm_score': 1, 'selected': False, 'text': "<p>Using custom composite controls has a point in a situation where you have a large web application and want to reuse large chunks in many places. Then you would only add child controls of the ones you are developing instead of repeating yourself.\nOn a large project I've worked recently what we did is the following:</p>\n\n<ul>\n<li>Every composite control has a container. Used as a wrapped for everything inside the control.</li>\n<li>Every composite control has a template. An ascx file (without the &lt;%Control%> directive) which only contains the markup for the template.</li>\n<li>The container (being a control in itself) is initialized from the template.</li>\n<li>The container exposes properties for all other controls in the template.</li>\n<li>You only use this.Controls.Add([the_container]) in your composite control.</li>\n</ul>\n\n<p>In fact you need a base class that would take care of initializing a container with the specified template and also throw exceptions when a control is not found in the template. Of course this is likely to be an overkill in a small application. If you don't have reused code and markup and only want to write simple controls, you're better off using User Controls.</p>\n"}, {'answer_id': 17916, 'author': 'Mark Cidade', 'author_id': 1659, 'author_profile': 'https://Stackoverflow.com/users/1659', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s another extension method that I use for custom rendering:</p>\n\n<pre><code> public static void WriteControls\n        (this HtmlTextWriter o, string format, params object[] args)\n { \n    const string delimiter = "&lt;2E01A260-BD39-47d0-8C5E-0DF814FDF9DC&gt;";\n    var controls  = new Dictionary&lt;string,Control&gt;();\n\n    for(int i =0; i &lt; args.Length; ++i)\n    { \n       var c = args[i] as Control; \n       if (c==null) continue;\n       var guid = Guid.NewGuid().ToString();\n       controls[guid] = c;\n       args[i] = delimiter+guid+delimiter;\n    }\n\n    var _strings = string.Format(format, args)\n                         .Split(new string[]{delimiter},\n                                StringSplitOptions.None);\n    foreach(var s in _strings)\n    { \n       if (controls.ContainsKey(s)) \n           controls[s].RenderControl(o);\n       else \n           o.Write(s);\n    }\n}\n</code></pre>\n\n<p>Then, to render a custom composite in the RenderContents() method I write this:</p>\n\n<pre><code>protected override void RenderContents(HtmlTextWriter o)\n{ \n    o.WriteControls\n         (@"&lt;table&gt;\n               &lt;tr&gt;\n                    &lt;td&gt;{0}&lt;/td&gt;\n                    &lt;td&gt;{1}&lt;/td&gt;\n               &lt;/tr&gt;\n             &lt;/table&gt;"\n            ,Text\n            ,control1);\n }\n</code></pre>\n'}, {'answer_id': 18663, 'author': 'Slavo', 'author_id': 1801, 'author_profile': 'https://Stackoverflow.com/users/1801', 'pm_score': 2, 'selected': False, 'text': "<p>Rob, you are right. The approach I mentioned is kind of a hybrid. The advantage of having ascx files around is that on every project I've seen, designers would feel most comfortable with editing actual markup and with the ascx you and a designer can work separately. If you don't plan on actual CSS/markup/design changes on the controls themselves later, you can go with a custom rendered control. As I said, my approach is only relevant for more complicated scenarios (and these are probably where you need a designer :))</p>\n"}, {'answer_id': 71331, 'author': 'Andrew Myhre', 'author_id': 5152, 'author_profile': 'https://Stackoverflow.com/users/5152', 'pm_score': 0, 'selected': False, 'text': '<p>You might be able to make use of this technique to make design-time easier:</p>\n\n<p><a href="http://aspadvice.com/blogs/ssmith/archive/2007/10/19/Render-User-Control-as-String-Template.aspx" rel="nofollow noreferrer">http://aspadvice.com/blogs/ssmith/archive/2007/10/19/Render-User-Control-as-String-Template.aspx</a></p>\n\n<p>Basically you create an instance of a user control at runtime using the LoadControl method, then hand it a statebag of some kind, then attach it to the control tree. So your composite control would actually function like more of a controller, and the .ascx file would be like a view.</p>\n\n<p>This would save you the trouble of having to instantiate the entire control tree and style the control in C#!</p>\n'}]
['https://Stackoverflow.com/questions/17532', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/832/']