Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET Database Programming Weekend Crash Course - J. Butler, T. Caudill.pdf
Скачиваний:
31
Добавлен:
24.05.2014
Размер:
3.32 Mб
Скачать

Session 29—Migrating From ASP to ASP.NET

295

</TR>

<%Next%>

</TABLE>

<%End Function%> </HEAD>

<BODY>

<%=CreateTable()%>

</BODY>

</HTML>

Instead, the code must be moved to a consolidated script block as shown in Listing 29-4, and Response.Write statements must be used to properly display the table.

Listing 29-4 Using script blocks in ASP.NET

<%@ Page Language=”VB” Debug=”False” Trace=”False” %> <HTML>

<HEAD>

<SCRIPT LANGUAGE=”vb” RUNAT=”Server”> Function CreateTable()

Dim x as Integer Response.Write(“<table>”) For x = 1 to 10

Response.Write(“<tr>”)

Response.Write(“<td>”) Response.Write(“Cell Number” & x) Response.Write(“</td>”) Response.Write(“</tr>”)

Next Response.Write(“</table>”) End Function

</SCRIPT>

</HEAD>

<BODY>

<%=CreateTable()%>

</BODY>

</HTML>

If you have not used script block based rendering using Response.Write() statements in your existing ASP applications, you will be in for some long nights migrating your application logic to ASP.NET. In fact, you will probably want to go ahead and perform a redesign, rather than simply performing a line-by-line conversion.

Syntax differences and language modifications

There are a ton of syntax modifications — mostly attached to the switch in supported languages — that you will need to become familiar with in ASP.NET. For the majority of you who have been coding primarily in VBScript, you will need to ramp up on the syntax differences in VB .NET.

296

Sunday Afternoon

Note

For a full listing of syntax changes that you will need to understand when using VB .NET in ASP.NET be sure to reference http://msdn.microsoft.com/vbasic/technical/upgrade/language.asp.

Enclosing function/subroutine calls in parentheses

In the previous examples, we illustrated how in ASP you can call functions or subroutines without using parentheses to encase the passed parameters. In VB .NET, you must encase all parameters in parentheses.

Strongly typed variables

ASP supported variant-oriented languages, meaning that you must declare objects as Variants and then at runtime assign them to objects, integers or any other data type desired. Since you now are using compiled languages, you must declare variables as a specific type and then convert them as needed. Table 29-1 shows some of the differences in variable declaration.

Table 29-1 Declaring Variables

 

VBScript in ASP

VB .NET in ASP.NET

Dim i

Dim i As Integer

 

 

Dim s

Dim s As String

 

 

Dim s1,s2

Dim s1, s2 As String

 

 

Dim o

 

 

 

Set o = CreateObject(“”)

Dim o As New Object()

 

 

Error handling

Using VB .NET or C#, you have access to the power of the Try...Catch...Finally statement to handle your errors. This provides significantly more control than is available using VBScript under ASP. Table 29-2 illustrates how errors are handled in VBScript and how they are handled in VB .NET.

Table 29-2 Handling Errors

 

VBScript in ASP

VB .NET in ASP.NET

Function WriteFile()

Function WriteFile()

 

 

On Error Resume Next

Try