Migrate Jscript To Jscriptnet Syntax

JScript.NET code implementation is much closer to the JScript implementation than VB.NET is to VBScript. Because JScript was more of an Object-Oriented language than VBScript. Jscript will resemble to the C# syntax that is used in this book than the VB syntax. The big difference that you will notice is in the variable declaration.

When converting an ASP page (*.asp) to an ASP.NET page (*.aspx) that has JScript in the server-side code, the first thing you will do is change the file extension to aspx. If you are going to access STA (Single Threaded Apartment) COM components,

MIGRATE JSCRIPT TO JSCRIPT.NET SYNTAX

then you need to set ASPCOMPAT=true for the @Page directive.

As with VB.NET, you can not access default properties on COM objects. If default properties are used, you have to find the explicit property that retrieves the default property and program using that explicit property. For example, if you have a label on a form, you can not access the Text property by the default property of the label. To access the Text property, you have to give an explicit reference to that property's data type — JScript.NET - var s : String = lblFirstName.Text; versus JScript -var s = lblFirstName;. Note that the String is explicitly declared when intitializing the variable.

MIGRATE JSCRIPT TO JSCRIPT.NET SYNTAX

D Open

SimpleQuery_JScript.aspx from the Code Templates directory.

-■ The ASPCOMPAT=true attribute is set in the @Page directive.

-■ The filePath statement appears in parentheses, so you do not have to change it.

< Scroll down the file.

D Open

SimpleQuery_JScript.aspx from the Code Templates directory.

-■ The ASPCOMPAT=true attribute is set in the @Page directive.

Scroll down the file.

-■ The filePath statement appears in parentheses, so you do not have to change it.

The set statement is not required in JScript, so there are no changes with initializing the object variable.

< Scroll down the file.

MIGRATE FROM ASP TO ASP.NET

Extra

JScript.NET is Microsoft's implementation of the ECMA 262 language. Improvements in JScript.NET — which is being developed in conjunction with ECMAScript Edition 4 — include true compiled code, typed and typeless variables, classes (with inheritance, function overloading, property accessors, and more), packages, cross-language support, and access to the .NET Framework. JScript.NET is a true object-oriented scripting language. Even though JScript.NET can now use classes, types, and other "industrial strength" language features for writing robust applications, it still keeps its "scripting" feel.

ECMAScript has strong roots with Netscape. Having JScript is beneficial for those who are inline with the standards body that defines ECMAScript. If you are looking for features and support, you will find more of this with the C# and VB languages. There are more books and sample code for these languages.

■ The statement oRs(Index) was changed to oRs(Index).Value.

SimpleQuery_JScript.aspx from the Web server.

■ The page appears properly as an ASP.NET page.

■ The statement oRs(Index) was changed to oRs(Index).Value.

SimpleQuery_JScript.aspx from the Web server.

■ The page appears properly as an ASP.NET page.

ML SERVER CONTROLS

BASIC HTML CONTROLS

NAMESPACE:

SYSTEM.WEB.UI.HTMLCONTROLS:

HtmlAnchor

working with the HTML <a> tag on the server.

HtmlButton

working with the HTML <button> tag on the server.

HtmlForm

working with the HTML <form> tag on the server.

HtmlGenericControl

working with tags not represented by .NET classes (eg. <span> and <div> tags).

HtmlImage

working with the HTML <img> tag on the server.

HtmlInputButton

working with the HTML <input type= button>, <input type= submit>, and <input type= reset> tags on the server.

HtmlInputCheckBox

working w

th the HTML <input type= checkbox> tag on the server.

HtmlInputFile

working w

th the HTML <input type= file> tag on the server.

HtmlInputHidden

working wi

th the HTML <input type= hidden> tag on the server.

HtmlInputImage

working w

th the HTML <input type= image> tag on the server.

HtmlInputRadioButton

working w

th the HTML <input type= radio> tag on the server.

HtmlInputText

working w

th the HTML <input type= text> and <input type= password> tags on the server.

HtmlSelect

working w

th the HTML <select > tag on the server.

HtmlTable

working w

th the HTML <table> tag on the server.

HtmlTableCell

working w

th the HTML <td> and <th> tags on the server.

HtmlTableCellCollection

working w

th a collection of HTML <td> and <th> tags on the server.

HtmlTableRow

working w

th the HTML <tr> tag on the server.

HtmlTableRowCollection

working w

th a collection of HTML <tr> tags on the server.

HtmlTextArea

working w

th the HTML <textarea> tag on the server.

ASP.NET QUICK REFERENCE

WEB SERVER CONTROLS

NAMESPACE:

SYSTEM.WEB.UI.WEBCONTROLS:

AdRotator

displaying an advertisement banner.

Button

posting data back to the server.

Calendar

displaying a one-month calendar.

CheckBox

creating a check box.

CheckBoxList

creating a multi-selection check box group.

DataGrid

creating a multi-column data bound grid.

DataList

creating a data bound list.

DropDownList

creating a drop-down list that contains a single selection.

HyperLink

creating a link for navigating to pages.

Image

creating a Web-compatible image.

ImageButton

creating an image to handle user click events.

Label

creating and manipulating static text.

LinkButton

creating hyperlink-style buttons that post back to the same page on which they originated.

ListBox

displaying a single-selection or multi-selection list box.

Panel

providing a container for other controls.

RadioButton

creating a radio button.

RadioButtonList

creating a radio button group.

Repeater

creating a data-bound list that renders a row for every row in the specified data source.

Table Web

creating a table and manipulating it programmatically.

TableCell

creating a table cell and manipulating it programmatically.

TableRow

creating a table row and manipulating it programmatically.

TextBox

creating single and multi-line text boxes.

VALIDATION SERVER CONTROLS

NAMESPACE:

SYSTEM.WEB.UI.WEBCONTROLS:

CompareValidator

comparing a user's entry against a constant value, against a property value of another control, or against a database value using a comparison operator (less than, equal, greater than, and so on).

CustomValidator

creating custom server and client validation code.

RangeValidator

checking that a user's entry is between specified lower and upper boundaries.

RegularExpressionValidator

checking that the entry matches a pattern defined by a regular expression.

RequiredFieldValidator

checking that the user does not skip an entry.

ValidationSummary

displaying a summary of all validation errors for all of the validation controls on a page.

GLOBAL.ASAX SYNTAX

<script language="VB" runat=server> Sub Application_OnStart()

1 Application startup code goes here... End Sub

Sub Session_OnStart()

1 Session startup code goes here... End Sub

Sub Session_OnEnd()

1 Session cleanup code goes here... End Sub

Sub Application_OnEnd()

1 Application cleanup code goes here... End Sub </script>

WEB.CONFIG SYNTAX

The following is an example of a template for the web.config file.

<?xml version="1.0" encoding="utf-8" ?> <configuration>

<system.web>

<!— DYNAMIC DEBUG COMPILATION

Set debugmode enable="true" to enable ASPX debugging. Otherwise, setting this value to false will improve runtime performance of this application.

<compilation defaultlanguage="c#" debug="true"

ASP.NET QUICK REFERENCE

WEB.CONFIG SYNTAX (CONTINUED)

<!— CUSTOM ERROR MESSAGES

Set mode enable="on" or "remoteonly" to enable custom error messages, "off" to disable. Add <error> tags for each of the errors you want to handle.

<customErrors mode="Off"

This section sets the authentication policies of the application. Possible modes are "Windows", "Cookie", "Passport" and "None"

authentication mode="Forms">

forms name=".ASPXUSERDEMO" loginUrl="login.aspx" protection="all" timeout="60" /> </authentication> <authorization>

<deny users="?" /> </authorization>

<!— application-level trace logging

Application-level tracing enables trace log output for every page within an application. Set trace enabled="true" to enable application trace logging, if pageOutput="true", the trace information will be displayed at the bottom of each page. Otherwise, you can view the application trace log by browsing the "trace.axd" page from your web application root.

<trace enabled="false" requestLimit="0" pageOutput="false" traceMode="SortByTime"

<!— session state settings

By default ASP+ uses cookies to identify which requests belong to a particular session, if cookies are not available, a session can be tracked by adding a session identifier to the URL. To disable cookies, set sessionState cookieless="true".

<sessionState mode="inproc"

stateConnectionString="tcpip=127.0.0.1:42424"

sqlConnectionString="data source=127.0.0.1;user id=sa;password="

cookieless="false"

timeout="20"

This section sets the globalization settings of the application.

<globalization requestEncoding="utf-8" responseEncoding="utf-8"

Visual Basic

C#

JScript

Dim x As Integer

int x;

var x :

int;

Public x As Integer = 10

int x = 10;

var x :

Visual Basic

C#

JScript

1 comment

// comment

// comment

x = 1 1 comment

/* multiline

/* multiline

Remi comment

comment */

comment */

ASSIGNMENT STATEMENTS

Visual Basic

C#

JScript

nVal = 7

nVal = 7;

nVal = 7;

IF...ELSE STATEMENTS

Visual Basic

C#

JScript

If nCnt <= nMax Then

if (nCnt <= nMax)

if(nCnt < nMax) {

nTotal += nCnt 1 Same as

{

nTotal += nCnt;

nTotal = nTotal + nCnt

nTotal += nCnt;

nCnt ++;

nCnt += 1 1 Same as nCnt

nCnt++;

}

= nCnt + 1

}

else {

Else

nTotal += nCnt;

nTotal += nCnt

nCnt -;

nCnt -= 1

};

End If

C#, VB, AND JSCRIPT LANGUAGE EQUIVALENTS

Visual Basic

JScript

Select Case n

switch(n) {

Case 0

case 0 :

MsgBox ("Zero")

Response.Write("Zero");

Visual Basic exits the Select at the end of a Case.

break;

Case 1

case 1 :

MsgBox (" One" )

Response.Write("One");

Case 2

break;

MsgBox (" Two" )

case 2 :

Case Else

Response.Write("Two");

MsgBox ("Default")

default :

End Select

Response.Write("Default");

C#

switch (n) {

case 0:

Console.WriteLine("Zero");

break;

case 1:

Console.WriteLine("One");

break;

case 2:

Console.WriteLine("Two");

break;

default:

Console.WriteLine("?");

}

Visual Basic

JScript

For n = 1 To 10

for (var n = 0; n < 10; n++) {

MessageBox.Show("The number is " & n)

Response.Write("The number is

+ n);

Next

}

For Each prop In obj

for (prop in obj){

prop = 42

obj[prop] = 42;

Next prop

}

C#

for (int i = 1; i <= 10; i++)

Console.WriteLine("The number is {0}", i);

Visual Basic C#

While n < 100 1 Test at start of loop while (n < 100)

End While 1

PARAMETER PASSING BY VALUE

BASICS EXAMPLES

PARAMETER PASSING BY VALUE

Visual Basic

C#

Public Sub ABC(ByVal y As Long) 1 The argument Y is

// The method:

passed by value.

void ABC(int x)

1 If ABC changes y, the changes do not affect x.

End Sub

ABC(x) 1 Call the procedure

You can force parameters to be passed by value,

// Calling the method:

regardless of how they are declared, by enclosing

ABC(i);

the parameters

in extra parentheses.

JScript

ABC((x))

ABC(i,j);

PARAMETER PASSING BY REFERENCE

Visual Basic

Public Sub ABC(ByRef y As Long) 1 The parameter of ABC

is declared by reference: 1 If ABC changes y, the changes are made to the value of x. End Sub

ABC(x) 1 Call the procedure

JScript

N/A (objects (including arrays) are passed by reference, but the object to which the variable refers to cannot be changed in the caller). Properties and methods change 0d in the callee are visible to the caller.

/* Reference parameters are supported for external objects, but not internal JScript functions */ comPlusObject.SomeMethod(&foo);

C#, VB, AND JSCRIPT LANGUAGE EQUIVALENTS

STRUCTURED EXCEPTION HANDLING

Visual Basic

C#

Try

// try-catch-finally

If x = 0 Then

try

Throw New Exception("x equals zero")

{

Else

if (x == 0)

Throw New Exception("x does not equal zero")

throw new System.Exception ("x equals zero");

End If

MessageBox.Show("Error: " & Err.Description)

throw new System.Exception ("x does not equal zero");

MessageBox.Show("Executing finally block.")

catch (System.Exception err) {

End Try

System.Console.WriteLine(err.Message);

finally {

System.Console.WriteLine("executing finally block");

}

JScript try {

if (x == 0) {

throw new Error(513, "x equals zero");

else {

throw new Error(514, "x does not equal zero")

}

catch(e) {

Response.Write("Error number: " + e.number + "<BR>");

Response.Write("Error description: " + e.message

finally {

+ "<BR>");

Response.Write("Executing finally block.");

}

SET AN OBJECT REFERENCE TO NOTHING

Visual Basic

C#

JScript

o = Nothing

o = null;

Was this article helpful?

0 0

Post a comment