Structural Changes

Snoops

   

 
Migrating to .NET: A Pragmatic Path to Visual Basic .NET, Visual C++ .NET, and ASP.NET
By Dhananjay  Katre, Prashant  Halari, Narayana  Rao  Surapaneni, Manu  Gupta, Meghana  Deshpande

Table of Contents
Chapter 7.   Migrating to ASP.NET II


Structural changes are those that affect the layout and coding style of ASP. There are a few of these you need to be aware of to ensure your code will work in ASP.NET.

Code Blocks

In ASP, you can declare subroutines and global variables in between the script delimiters. In ASP.NET, this is no longer allowed. All of the subroutines, functions, and variables must be declared inside a <script> block.

The following code is a typical ASP code in which a subroutine is declared. Notice that the declaration is done within the <% %> directives.

 graphics/icon01.gif <%        Dim x        Dim str        Sub addNum(i, j)           Response.Write "Addition results : " & i+j        End Sub  %> 

This code works perfectly well in ASP but crashes in ASP.NET. It has to be modified in ASP.NET to make it work. It would be written as follows :

 graphics/icon01.gif <script language = "vb" runat = "server">     Dim x     Dim str     Sub addNum(i as Integer, j as Integer)        Response.Write "Addition results : " & i+j     End Sub  </script> 

A page can contain more than one <script> block, but the programming language must be the same in all script blocks on a page. All code that is included within the <script> </script> tags, except global declaration statements, must be encapsulated within sub-procedures.

Render Functions

A render function is basically a subroutine that contains chunks of HTML embedded throughout its body. A typical render function follows:

 graphics/icon01.gif <%Sub renderFunction()%>     <H3> This is a typical render function. </H3>  <%End Sub%>     <%renderFunction%> 

Although using these types of functions is convenient , this type of coding is no longer allowed in ASP.NET. Such functions quickly become unreadable and unmanageable when one starts to mix and match code and HTML in this manner. The simplest way to make this work in ASP.NET is replace your HTML outputs with calls to Response.Write as follows:

 graphics/icon01.gif <script language="vb" runat="server">     Sub renderFunction ()        Response.Write("<H3> This is a typical render        function. </H3>")     End Sub  </script>  <%     Call renderFunction ()  %> 

Use of Scripting Languages

In ASP, there were very few choices for a scripting language. There was either VBScript or Jscript, and the developer was free to mix and match blocks of script on the same page at will.

ASP.NET offers options such as C#, Visual Basic .NET, and JScript. Notice that Visual Basic .NET was listed instead of VBScript. This is because VBScript does not exist in the .NET platform. It has been fully replaced by Visual Basic .NET. The default language is Visual Basic, but any another language can be declared as the default language for the page by placing a directive at the top of the page as follows:

 <%@ Page Language="C#"%> 

Although .NET gives the freedom to choose between any of these languages, it is important to note that it is no longer possible to mix languages on the same page as in ASP. It is certainly possible to have Page1.aspx of your application contain C# code while Page2.aspx of the same application contains Visual Basic .NET code. You just cannot mix them together in a single page.

The language can also be declared in a <script language = > block. If different languages are declared in separate script blocks on the same page, an error will be thrown. Although only one language can be used on a single page, user controls that you use on a page can each be written in different languages.

ASP.NET offers support to all CLS-compatible programming languages supported by the common language runtime, including Visual Basic, C#, and the Managed Extensions for C++. VBScript is no longer supported; however, VBScript syntax is very similar to Visual Basic .NET syntax, so any changes required to existing code should be minor.

If the existing ASP pages use JScript as the scripting language, migration to ASP.NET can be achieved with minimum effort because .NET is based on the object-oriented approach. A scripting language such as JScript that supports OOP will make things easier during migration. If the scripting language used is VBScript, the migration efforts will include early binding of objects for .NET as against late binding as used in VBScript.

New Page Directives

The use of directives in ASP requires that you place all of them on the first line of a page within the same delimiting block. In ASP the directives are written as:

 <%@LANGUAGE="VBSCRIPT" CODEPAGE="932"%> 

In an ASP.NET file, directives can be located anywhere , but standard practice is to place them at the beginning of the file. Case is not important in ASP.NET directive statements, and quotes are not required around the attribute values. The Page directive can be written as follows:

 <%@PAGE LANGUAGE="VB" EXPLICIT="TRUE"%> 

or without quotes :

 <%@PAGE LANGUAGE=VB EXPLICIT=TRUE%> 

The directives in ASP such as LANGUAGE , ENABLESESSIONSTATE , CODEPAGE , LCID , and TRANSACTION can all be written as attributes of the Page directive in ASP.NET as shown in Table 7-8. Table 7-9 gives a list of new directives provided by .NET. The Application directive is used only in the global.asax file.

Table 7-8. Page Directives

ASP

ASP.NET

Description

@ LANGUAGE

@ PAGE LANGUAGE

Specifies the language used when compiling all inline rendering (<% %> and <%= %>) and code declaration blocks within the page.

@ ENABLESESSIONSTATE

@ PAGE ENABLESESSIONSTATE

Defines session-state requirements for the page.

@ CODEPAGE

@ PAGE CODEPAGE

Indicates the code page value for the response.

@ LCID

@ PAGE LCID

Defines the locale identifier for the Web Forms page.

@ TRANSACTION

@ PAGE TRANSACTION

Indicates whether transactions are supported on the page.

Table 7-9. Page Directives Provided by ASP.NET

Directive

Description

Example

@ Page

Defines page-specific attributes used by the ASP.NET page parser and compiler. Can only be included in .aspx files.

<%@ Page Language="VB" ContentType= "text/xml" %>

@ Control

Defines control-specific attributes used by the ASP.NET page parser and compiler. Can only be included in .ascx files (user controls).

<%@ Control Language="VB" EnableViewState= "false" %>

@ Import

Explicitly imports a namespace into a page or user control.

<%@ Import Namespace="System. Collections" %>

@ Implements

Declaratively indicates that a page or user control implements a specified .NET Framework interface.

<%@ Implements Interface="System.Web. UI.IPostBackEvent Handler" %>

@ Register

Associates aliases with namespaces and class names , thereby allowing user controls and custom server controls to be rendered when included in a requested page or user control.

<%@ Register Tagprefix="MyTag" Namespace="MyCompany: MyNameSpace" Assembly= "MyAssembly" %>

@ Assembly

Declaratively links an assembly to the current page or user control.

<%@ Assembly Name ="MyAssembly. dll" %>

@ OutputCache

Declaratively controls the output caching policies of a page or user control.

<%@ OutputCache Duration="10" VaryByParam="none" %>

@ Reference

Declaratively links a page or user control to the current page or user control.

<%@ Reference Control="MyControl. ascx" %>

@Application

Defines application-specific attributes used by the ASP.NET application compiler.

<%@ Application Inherits="MyApp.Object" Description= "Our app" %>


Snoops

   
Top


Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
Migrating to. NET. A Pragmatic Path to Visual Basic. NET, Visual C++. NET, and ASP. NET
ISBN: 131009621
EAN: N/A
Year: 2001
Pages: 149

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net