User Controls - Page 2
August 2, 2002
The simplest form of reuse in classic ASP is the include file.
By adding the following directive:
<!-- #include file = "filename.inc" -->
ASP developers could place the contents of the specified file
inline with the page in which the directive appeared. Unfortunately, this
reuse technique is a bit crude and sometimes makes applications harder to
debug.
While ASP.NET still supports include files, a better way to
provide the same kinds of reuse is through a new feature called user controls. User controls consist of HTML, a
server-side script, and controls, in a file with the .ascx file
extension. When added to a Web Forms page, ASP.NET treats user controls as
objects; these user controls can expose properties and methods like any other
object. The rendered output of user controls can also be cached to improve
application performance.
Example
6-1 shows a simple user control that provides navigational links to other
examples in this chapter. The user control appears in each example page to
demonstrate how the use of a user control can provide a single point for
modifying such frequently used elements as headers, footers, and navigation
bars.
Example 6-1: Nav.ascx <%@ Control Language="vb" %>
<table cellpadding="0" cellspacing="0">
<tr>
<td valign="top">
<strong>Navigation Bar</strong><br/>
<hr width='80%'>
<a href="NavBarClient.aspx"
onmouseover="img1.src='node_rev.jpg';"
onmouseout="img1.src='node.jpg';">
<img border='0' align='absMiddle' alt='NavBar Client'
src='node.jpg' id='img1' name='img1'></a>
<a href="NavBarClient.aspx"
onmouseover="img1.src='node_rev.jpg';"
onmouseout="img1.src='node.jpg';">NavBar Client</a>
<hr width='80%'>
<a href="UCClient.aspx"
onmouseover="img2.src='alt_node_rev.jpg';"
onmouseout="img2.src='alt_node.jpg';">
<img border='0' align='absMiddle' alt='User Control Client'
src='alt_node.jpg' id='img2' name='img2'></a>
<a href="UCClient.aspx"
onmouseover="img2.src='alt_node_rev.jpg';"
onmouseout="img2.src='alt_node.jpg';">User Control Client</a>
<hr width='80%'>
<a href="BlogClient.aspx"
onmouseover="img3.src='node_rev.jpg';"
onmouseout="img3.src='node.jpg';">
<img border='0' align='absMiddle' alt='Blog Client'
src='node.jpg' id='img3' name='img3'></a>
<a href="BlogClient.aspx"
onmouseover="img3.src='node_rev.jpg';"
onmouseout="img3.src='node.jpg';">Blog Client</a>
<hr width='80%'>
<a href="BlogAdd.aspx"
onmouseover="img3.src='alt_node_rev.jpg';"
onmouseout="img3.src='alt_node.jpg';">
<img border='0' align='absMiddle' alt='Add New Blog'
src='alt_node.jpg' id='img3' name='img3'></a>
<a href="BlogAdd.aspx"
onmouseover="img3.src='node_rev.jpg';"
onmouseout="img3.src='node.jpg';">Add New Blog</a>
<hr width='80%'>
</td>
</tr>
</table>
With the exception of the @ Control
directive, which is not strictly required, the code in Example
6-1 consists exclusively of HTML and client-side script (for performing a
simple mouseover graphics switch). However, the user control could just as
easily contain server controls and/or server-side script to perform more
complicated tasks.
The @ Control directive performs
essentially the same task as the @ Page directive,
only for user controls. Chapter 3 lists the attributes of the @ Page and @ Control
directives and the purpose of each.
The advantage of using a user control for this type of
functionality is that it places all of our navigation logic in a single
location. This placement makes it considerably easier to maintain the
navigation links for a site. If you used ASP.NET's built-in server controls
instead of raw HTML in your navigation user control, you could manipulate
those server controls programmatically from the page on which the control is
used. For example, you could hide the link to the page that's currently
displayed or highlight it in some fashion.
The disadvantage of a user control is that it is not reusable
across multiple sites. It's also not usually a good idea to tightly couple
user interface elements and data, as this control does, because doing so tends
to reduce the reusability of a control. Later in this chapter, you'll see how
to improve this user control by turning it into a custom server control.
User controls are made available to a page through the use of
either the @ Register directive, which prepares a
user control on a page declaratively (i.e., using a tag-based syntax like
server controls), or to be included programmatically using the LoadControl
method of the TemplateControl class (from which
both the Page class and the UserControl class derive).
ASP.NET in a Nutshell
ASP.NET in a Nutshell
User Controls (Cont.) - Page 3
|