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

Jeffrey Palermo [MVP]

Software management consultant and CTO, Headspring Systems

How to decide: user controls vs. composite controls vs. rendered custom controls

There are several ways to make your own server controls in ASP.NET.  You may have wondered which type you should make.  There are two basic types:
  • user controls
  • custom controls
Within custom controls, you have rendered controls, and composite controls.  A composite control could merely inherit from another control and change or add something.  More often, though, it creates many child controls to render inside itself.

A User Control is the easiest to make, and it was originally going to be called "pagelets" because the creation experience is pretty close to that of a page.  You have the html view and a code-behind.  It's just a snippet of what would go on a page.  You can lay out your html and add other server controls inside, and then control them with your code-behind.  This type of control should be considered first.  If you need a control that is specific to a particular application, this type of control is for you.

A custom control is all code and is compiled into an assembly.  The main benefit is that the assembly can be distributed and shared among many applications.  If you don't need to share controls between applications, then it may be hard to justify using this type of control.  This control is more time-consuming to create.  One valid reason may be that you need a control all over in your application.

Within the custom control umbrella, you have the rendered controls and composite or inherited controls.  I prefer the inherited controls myself.  Often, I'll inherit from a Panel (a <div/>) and add other controls as children.  Another method some prefer is rendered controls.  This is when you inherit from Control and override the Render method and output all html yourself.  This can quickly become cumbersome and if very error-prone.  I would consider this a last resort.  In some cases, a performance argument is made, and your decision will have to be based on the application environment.

In summary, if you need to distrubute controls in an assembly, you will have to use custom controls and decide between rendered or composite.  If all your controls will stay with the application, user controls are the most compelling answer.

A must read for every ASP.NET developer is Developing ASP.NET Server Controls and Components.

In the current events space, contratulations to .Net Junkies for upgrading their blogging engine to Community Server.

Update:  Thank you Fregas for the correction.  I'm very excited about declaratively setting properties on user controls.


Comments

Fregas said:

Actually, you can declaritively set properties in user controls. if you have a user control called "DisplayUser", you can set the firstname property with something like this:

<uc1:DisplayUser runat="server" FirstName="Bob" />
# July 8, 2005 12:05 PM

Jeffrey Palermo said:

As I mentioned before, I'm at a new company, and I'm one of the .Net developers working on Shadows.com.&amp;nbsp;...
# July 19, 2005 12:56 PM

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