ASP.NET
  Home arrow ASP.NET arrow Page 7 - Migrating from ASP to ASP.NET
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP.NET

Migrating from ASP to ASP.NET
By: Dada Kalander
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 47
    2004-01-12

    Table of Contents:
  • Migrating from ASP to ASP.NET
  • Active Server Pages: Cons
  • What is the .NET Framework?
  • ASP.NET Pros
  • Language Support
  • Server Controls
  • User Controls
  • Caching
  • Session Management
  • Security
  • Recomendation for Best Practices

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More ASP.NET Articles
    More By Dada Kalander


       · Your second paragraph repeats the last half of the first.
     

    ASP.NET ARTICLES

    - Adding Content to a Static ASP.NET Website
    - Building a Static ASP.NET Website in a Basic...
    - Develop Your First ASP.NET Website with Visu...
    - Run ASP.NET in Windows XP Home with Cassini ...
    - How to Test a Web Application
    - How to Add Code and Validation Controls to a...
    - Working in Source and Split Views to Build a...
    - How to Build a Web Form for a One-Page Web A...
    - How to Develop a One-Page Web Application
    - An ASP.NET Web Application in Action
    - Developing ASP.NET Web Applications
    - An Introduction to ASP.NET Web Programming
    - Introduction to the ADO.NET Entity Framework...
    - Completing an In-Text Advertising System und...
    - Programming an In-Text Advertising System un...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek