Choosing Between User Controls and Custom Controls
Monday, 09 March 2009
HTML clipboard
ASP.NET provides two models of creating Web controls – user controls and
custom controls. This article assumes basic knowledge of the two models and will
not provide detailed information on writing controls. Instead, this article will
present the reasons why one should choose one model over the other.
User Controls
User controls are authored in the same fashion as a standard Web Form. This
makes user controls relatively easy to create, especially when aided by a visual
designer such as Visual Studio .NET. Hence, given the same WYSIWYG and
declarative environment as an ASP.NET page, user controls can be created with or
without a code-behind file, and can handle their own events independent of the
parent page. Design-time support for user controls, however, is limited. User
controls are represented only by a dull placeholder and properties cannot be set
via the Properties window. Also, user controls cannot be added to the Toolbox;
sharing of user controls is achieved through placing the necessary user control
files in each Web application directory.
Custom Controls
Unlike user controls which are authored in the same fashion as a Web Form,
custom controls are compiled and distributed in binary format. Control authors
must create their own classes which subclass from System.Web.UI.Control either
directly or indirectly by subclassing another control. Custom controls are
created without the aid of a designer and require the author to overcome a much
steeper learning curve. On the other hand, custom controls provide strong
designer-time support. Once compiled, custom controls can be added to the
Toolbox and be used the same fashion as the other controls that ship with the
.NET SDK such as the TextBox and Button controls. Furthermore, custom controls
can expose properties which can easily be set by page developers using the
Properties window of the visual designer. Finally, custom controls allow authors
to extend or modify the functionality provided by existing controls.
Performance
With regards to performance, neither has a distinct advantage over the other.
Both derive from System.Web.UI.Control and both are compiled into assemblies.
Hence, performance is not a factor to consider when choosing between user and
custom controls.
Conclusion
In conclusion, the single most important factor is how the control will be
used – will the control be application specific or generic and redistributable?
If the control is application specific and contains a lot of static layout, such
as a site header and footer, a user control would make sense. If the control is
generic, redistributable, and contains dynamic layout, such as the server
controls that ship with the .NET SDK, then a custom control would be more
suitable.
Summary
Ease of Authoring – User controls win hands down
Design-Time Support – Custom controls can be added to the toolbox, can expose
simple or complex properties, and can be bound to a data source
Deployment – Custom controls are compiled into binary format and are easily
deployed across applications
Layout – User controls suite static layout while custom controls are better
suited for complex layouts