CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Jeffrey Palermo (.com)

Blog moved to www.jeffreypalermo.com

ASP.NET Rendered Controls vs. Composite Controls - I prefer to avoid rendered controls - level 200

Here, I'll present a simple example of the same control using the rendered method and the composite method.  For those new to custom web controls, a rendered control is a class inherited from "Control" or "WebControl" that overrides the "Render" method in order to sned custom markup to the browser.  A composite control uses other controls in a hierarchy and leaves the rendering to each control. 

Here is a simple rendered control:

    6     public class RenderedControl : Control

    7     {

    8         protected override void Render(HtmlTextWriter writer)

    9         {

   10             writer.AddAttribute(HtmlTextWriterAttribute.Id, "myID");

   11             writer.RenderBeginTag(HtmlTextWriterTag.Div);

   12             writer.AddAttribute(HtmlTextWriterAttribute.Id, "innerID");

   13             writer.RenderBeginTag(HtmlTextWriterTag.Span);

   14             writer.Write("This text is the meat of this control.");

   15             writer.RenderEndTag();

   16             writer.RenderEndTag();

   17         }

   18     }

It's pretty straight-forward, and the output will always be this: 

   18 <div id="myID">

   19     <span id="innerID">This text is the meat of this control.</span>

   20 </div>

Here is the same thing using the composite method:

    6     public class CompositeControl : Panel

    7     {

    8         protected override void CreateChildControls()

    9         {

   10             base.CreateChildControls ();

   11 

   12             this.ID = "myID";

 

   13             Label lbl = new Label();

   14             lbl.ID = "innerID";

   15             lbl.Text = "This text is the meat of this control.";

 

   16             this.Controls.Add(lbl);

   17         }

   18     }

With the composite method, the developer doesn't have to worry about the low level markup being rendered, and he can depend on the richer object-oriented contract of each child control.  With this method, the developer overrides the CreateChildControls() method and adds child controls.  All these child controls will render themselves.  The exact same markup renders.

In my opinion, using the composite control method optimizes development time, and that is the most expensive more often than not.  I don't give weight to the performance argument because the difference in performance is on the millisecond scale.  The moment you call out-of-process to a database or the file system, you accept a performance hit of much more magnitude; therefore, the performance difference of these two methods is negligible.  The composite control method is more maintainable, and maintenance of an application always costs more.



Comments

BlackTigerX said:

uff... for a minute I thought you preferred the "rendered controls"...

the composite control uses rendering behind the scenes anyway, but all the dirty work is already done. reuse code!
# October 18, 2005 6:56 PM

Jeffrey Palermo said:

Yes, reuse. One point I failed to mention: The included web controls are very smart. They will render differently based the browser capabilities, and they know exactly how to maintain viewstate if need be. By ignoring that and rendering html myself, I would have to take on a lot more responsibility. I'd rather reuse Microsoft's work than attempt try create my own rendering logic.
# October 18, 2005 8:48 PM

John Papa said:

I agree that composite controls are the way to go. I prefer them myself, never liked formatting and spitting out HTML much.

I guess one counterargument that I hear is that the rendered controls can be more efficient since they don't involve nearly as many objects instantiations. But frankly, I have not seen the performance of composite controls be a factor in my projects. It might truly be an issue, but I just have not seen it in my limited usage of it. Because of that, I opt for readability and maintainability of the composite controls.
# October 19, 2005 12:39 AM

About Jeffrey Palermo

Jeffrey Palermo is a software management consultant and the CTO of Headspring Systems in Austin, TX. Jeffrey specializes in Agile coaching and helps companies double the productivity of software teams. Jeffrey is an MCSD.Net , Microsoft MVP, Certified Scrummaster, Austin .Net User Group leader, AgileAustin board member, INETA speaker, INETA Membership Mentor, Christian, husband, father, motorcyclist, Eagle Scout, U.S. Army Veteran, and Texas A&M University graduate. Check out Devlicio.us!

This Blog

Syndication

News

Headspring Systems

View Jeffrey Palermo's profile on LinkedIn

See my new blog at .jeffreypalermo.com