User Controls
User controls provide a way to encapsulate site functionality in ASP.NET. Because
user controls are self-contained in standalone files, you can think of them as
a "black box." Variables are explicitly passed to user controls by
using public properties. Similarly, for user control methods to be accessed outside
the scope of the user control, they must be declared as public. If the user control
contains HTML or Web Form information, then it will be produced inline, where
the user control was included in the calling form. In this way, a user control
is like any other server control.
User controls are self-contained entities, and they support fragment caching.
By using a user control and fragment caching, it is possible to cache the output
of a single piece of functionality on a page. For instance, if a page in an application
contains a menu and highly personalized user content, it would be difficult to
send the entire page to the output cache because of the high number of potential
permutations of the personalized content. For instance, if a page welcomes a
user by name, it would be difficult to cache the entire page since a separate
copy of the page would need to be stored in memory for each user name. Encapsulating
certain pieces of the page, such as the menu, into a user control and then turning
on fragment caching for the user control increases the overall performance of
the page.
Because user controls can be programmatically manipulated, several developers
can use user controls to work on the design of an ASP.NET application simultaneously.
Because each user control is self-contained, each developer can work on his or
her part of the puzzle and not worry too much about interfering with someone
else's code.
Visual Studio and the Code Behind Method
Currently, there are two major paradigms for designing ASP.NET Web Forms:
-
One method is to insert server-side code on a page that contains all the
HTML and client-side code. This method of building pages should be familiar
to ASP developers.
-
Because ASP.NET Web Forms are compiled, you can place presentation logic
such as HTML and any Web controls in one file and any server-side code (such
as C# or Visual Basic.NET) in a second file. At compile time, the two pages
are compiled as one entity. This method of designing ASP.NET Web Forms is called
code behind.
Although many users might be reluctant to use this method, the code behind
method offers some benefits. Presentation code such as HTML is neatly separated
from any page logic being performed. This makes it easy for a development group
in charge of graphics and layout to work independently of a group in charge of
back-end development. Visual Studio.NET uses this code behind method of development.
In fact, it doesn't support the traditional in-line code methodology at
all.
If you have done significant development in previous versions of ASP, you might
find code behind difficult to work with at first, especially when combined with
all the other new features in ASP.NET. It's definitely worth learning to
use, however, Visual Studio.NET impressively shortens development time.