Migrating from ASP to ASP.NET - User Controls
(Page 7 of 11 )
ASP Web developers are quite familiar with the concept of include files. These files allow frequently used code or HTML (such as headers or footers) to be stored in text files that can be re-used throughout a Web site. An example of the ASP syntax used to reference an include file named Header.inc is shown below:
<!--#Include
Virtual=”/Includes/Header.inc” -->
While include files are quite powerful and can greatly simplify the maintenance of a Website, they do have a few shortcomings. First, ASP does not provide a convenient way to dynamically add an include file into a Web page. To illustrate this, consider the common scenario where several include files (weather, news, markets, etc.) need to be dynamically added into a Web page depending upon a user’s selected preferences. To handle this task each include file is typically added into a large If statement or Select Case code block which makes it more difficult for other include files to be dynamically added to an application in the future. An example of this is shown below:
‘Read in user preference from cookie
or db into
'pref variable
Dim pref
pref = GetUserPref()
Select Case pref
Case “Weather”
<!--#Include
Virtual=”/Includes/Weather.inc” -->
Case “News”
<!--#Include
Virtual=”/Includes/News.inc” -->
Case “Markets”
<!--#Include
Virtual=”/Includes/Markets.inc” -->
Case Else
<!--#Include
Virtual=”/Includes/Default.inc” -->
End Select
Second, include files do not make it easy for HTML coders to supply additional information that the include file may use to alter the way it functions or to alter the HTML that it generates. As an example, there is no way to supply the starting date to an include file that creates an HTML calendar using pure HTML code (without resorting to using the QueryString anyway). Instead, VBScript or JScript code must be used to assign the start date to a variable in the include file. Finally, include files are not compiled or automatically cached, which is not optimal for performance and scalability. Although ASP.NET still supports the concept of include files, it offers a more powerful object-oriented alternative called “user controls.” User controls are special files that can dynamically be loaded with very little programming code, allow HTML coders to easily supply additional data needed by the control, and offer better performance through compiled code.
To create a user control, a special directive named Control must be added at the top of the control’s code: <%@ Control %>. The user control file must then be saved with an .ascx extension. The user control can define properties (items that help describe the user control or allow data to be passed to it) that can make it easier for HTML coders to perform advanced tasks without actually understanding programming.
To add a user control into an ASP.NET Web page, two different coding steps must be performed. First, a special Register directive must be added to the top of the ASP.NET page. This directive has several attributes as shown below:
<%@ Register TagPrefix=”Acme”
TagName=”Calendar”
Src=”UserControls/Calendar.ascx” %>
After the user control is registered, it can be added anywhere within an ASP.NET Web page by referencing the defined TagPrefix and TagName values and by adding the runat attribute:
<Acme:Calendar id=”ucCalendar”
runat=”Server” />
Adding the above code into the Web page’s HTML would cause a user control named Calendar.ascx to be run that would output calendar HTML code. Assuming the Calendar.ascx user control defined a property named StartDate, the initial date displayed by the calendar could be set by using the following code:
<Acme:Calendar id=”ucCalendar”
StartDate=”10/2003” runat=”Server" />
Many other useful tasks can be performed with user controls including caching (caching is covered next) and dynamically loading them into a Web page. Since this section started out by showing how much code would be required to dynamically load include files in ASP, let’s finish the section off by showing the same process using ASP.NET user controls. Figure 5 demonstrates how to dynamically load a user control into an ASP.NET Web page using either VB.NET or C# code.
C#:
//Dynamically load a user control
UserControl ctl =
(UserControl)Page.LoadControl(path);
Page.Controls.Add(ctl);
VB.NET:
‘Dynamically load a user control
Dim ctl
as UserControl = _
CType(Page.LoadControl(path),UserControl)
Page.Controls.Add(ctl)
Figure 5. User controls can dynamically be loaded into an ASP.NET Web page by using the LoadControl() method associated with the Page class. This technique is much easier to work with as compared to “classic” ASP and provides greater flexibility.Next: Caching >>
More ASP.NET Articles
More By Dada Kalander