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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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


     

    ASP.NET ARTICLES

    - Developing a Mini ASP.NET AJAX Server Centri...
    - Disadvantages of the ASP.NET MVC Framework
    - Advantages of the ASP.NET MVC Approach
    - ASP.NET Web Forms Weaknesses
    - ASP.NET Web Forms Meets ASP.NET MVC
    - Source Code for Saving and Retrieving Data w...
    - Using GridView to Save and Retrieve Data wit...
    - Handling Dynamic Images in ASP.NET 3.5 AJAX ...
    - Retrieving Data with AJAX and the GridView C...
    - Playing with Images in ASP.NET 3.5 AJAX Appl...
    - Saving and Retrieving Data with AJAX
    - Enhancing PHP Via the ASP.NET AJAX Framework...
    - Enhancing PHP Programming with the ASP.NET A...
    - Classes and ASP.NET AJAX
    - Using ASP.NET AJAX

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT