Datasheet
When to Use a User Control
User controls should be your first choice for creating Web Parts where the control will be used on a sin-
gle Web site only and isn’t extending some other control. There are many benefits to using user controls:
❑ The drag-and-drop design interface supported by user controls is the simplest and fastest way
to create your controls’ user interface.
❑ You can put any amount of server-side code behind a Web user control, so you can have your
Web Part perform any server-side processing that you want.
❑ You can give your Web Part custom properties, methods, and events, increasing the flexibility of
the Web Part.
❑ You can include client-side code and HTML in your Web Part along with ASP.NET WebForm
controls and server-side code just by switching to HTML view. This also lets you take advantage
of Visual Studio .NET’s support for writing client-side code (including IntelliSense).
❑ In addition to being more powerful than pure HTML, ASP.NET WebForm controls generate
their HTML at run time and can configure the HTML they generate to match the device that is
requesting them.
Because a user control can be used in a single project only, if you want to use a user control on two Web
sites, you have to copy the control from the project that it was created in and paste it into the other project
that it will be used in. This means that you will eventually end up with multiple copies of the same user
control. When that happens you lose two of the benefits of components:
❑ Standardization: Inevitably, the multiple copies of the user control will start to diverge. Each
version of the user control will be tweaked to work with the project that it’s being used in. As
a result, the functionality embedded in the user control will start to work differently in the
different versions.
❑ Productivity: The first loss in productivity occurs because multiple developers continue to
develop the different versions of the user control. The second loss occurs because developers will,
eventually, have to take time to reconcile the different versions of the user control. For instance,
when a bug is discovered, or a change is required because of changes in the organization, or
the opportunity for an enhancement is recognized, developers will have to chase down all the
versions of the control to make the change.
When to Use a Custom Control
Custom controls can do anything that a user control can do — it just takes longer to build them. (This
makes custom controls an excellent choice if you are paid by the hour.) When building a custom control
you may find that you have to write the server-side code that generates the client-side portion of your
Web Part, including any HTML. This means that you give up the way that ASP.NET’s standard controls
automatically adapt their HTML to the client that requested them.
You can still use ASP.NET WebForm controls in your Web custom control, but you must write code to
add those WebForm controls to your user interface rather than using drag-and-drop. The same is true of
any client-side code that you add to your Web Part: The client-side code must be generated from your
server-side code, rather than typed into the HTML view of a Web Page. Without the benefit of the Visual
15
Creating Your Own Controls
05_57860x ch01.qxd 10/4/05 9:26 PM Page 15