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
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
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.