Q: What's the difference between User
Controls and Custom Controls?
A: There are two main categories
of creatable Web controls. User controls are more simple to create, but custom controls
are more simple to use. The extra effort that goes into creating custom controls
pays off for the developer using the control at design time.
User controls are the natural evolution of include files, which are
well known by old school ASP developers. If you're still using server-side include
files in ASP.NET, it's time to leave them behind and dig into user controls, which
are better in virtually every way. User controls are little more than a snippet
of a Web page that can be dragged and dropped anywhere to duplicate bits of user
interface and the associated functionality. (In fact, it's not difficult to convert
an entire Web page into a user control.) They are object-oriented, so public properties
can be added, as well as methods and events, to allow them to easily communicate
with the page and other controls on the page.
User controls are saved with an ASCX file extension. Although they
can be easily reused on any of the pages within the project in which they were created,
there is no good way to reuse them in a different project. Another downside to user
controls is that they only appear on the page at run time; at design time they appear
on the page as an ugly gray box, making it difficult to envision how they'll appear
to your users.
Figure 1 shows the HTML source code for a basic
sidebar menu user control. A standard control like this is a navigational staple
needed by virtually every page in a Web site. It would be wasteful to manually re-create
such a structure over and over again for each page. Instead, simply drag this control
from the Visual Studio.NET solution explorer window onto each page as needed. This
example does not include any code-behind logic, but any user control can have a
code-behind file of its own to contain any necessary server-side code. Figure 2
shows how the control looks on a Web page that includes a CSS style sheet to enhance
the appearance.
<%@ Control Language="vb" AutoEventWireup="false" %>
<TABLE
id="Table1"
style="WIDTH: 136px;
HEIGHT: 99px"
cellSpacing="1"
cellPadding="1"
width="136"
border="1">
<TR>
<TD
align="center">
<asp:HyperLink
id="HyperLink1"
runat="server"
NavigateUrl="Home.aspx">Home</asp:HyperLink>
</TD>
</TR>
<TR>
<TD
align="center">
<asp:HyperLink
id="HyperLink2"
runat="server"
NavigateUrl="Products.aspx">Products</asp:HyperLink>
</TD>
</TR>
<TR>
<TD
align="center">
<asp:HyperLink
id="HyperLink3"
runat="server"
NavigateUrl="Services.aspx">Services</asp:HyperLink>
</TD>
</TR>
<TR>
<TD
align="center">
<asp:HyperLink
id="HyperLink4"
runat="server"
NavigateUrl="Contact.aspx">Contact Us</asp:HyperLink>
</TD>
</TR>
</TABLE>
Figure 1: (Above) A basic reusable side menu user control
can be as simple as this. This control was created completely by dragging and dropping
controls from the Visual Studio.NET toolbar onto an .ASCX user control page.

Figure 2: (Above) User controls are great for defining the
layout for standard sections of Web pages. When combined with style sheets, they
can morph to match the look and feel of any Web page on which they are hosted.
Custom controls can do everything that user controls can do - and
much more. The biggest drawback is that they are more challenging and complex to
create. Drag and drop is not supported for the creation of these controls. Therefore,
all the HTML they output must be generated via code. Pretty much all the
articles & controls on this web site are examples of custom controls. In addition
to run-time functionality without limits, custom controls support rich design-time
functionality. They can appear on a page at design time the same way they'll appear
at run time, or differently if preferred. They can be adjusted to change how they'll
appear in the toolbox, and how the properties will appear in the properties window.
If the properties window doesn't support the desired functionality, it can be extended
with custom popup dialog boxes and other UI tricks. Custom controls are easily distributed
and can be enhanced with help documentation and licensing functionality, allowing
them to be sold to other developers.