- •Preface
- •Who Should Read This Book
- •Organization and Presentation
- •Contacting the Authors
- •Acknowledgments
- •Contents
- •Introduction
- •Why Microsoft .NET?
- •The Microsoft .NET Architecture
- •Internet Standards
- •The Evolution of ASP
- •The Benefits of ASP.NET
- •What Is .NET?
- •.NET Experiences
- •.NET Clients
- •.NET Services
- •.NET Servers
- •Review
- •Quiz Yourself
- •Installation Requirements
- •Installing ASP.NET and ADO.NET
- •Installing the .NET Framework SDK
- •Testing Your Installation
- •Support for .NET
- •Review
- •Quiz Yourself
- •Designing a Database
- •Normalization of Data
- •Security Considerations
- •Review
- •Quiz Yourself
- •Creating a Database
- •Creating SQL Server Tables
- •Creating a View
- •Creating a Stored Procedure
- •Creating a Trigger
- •Review
- •Quiz Yourself
- •INSERT Statements
- •DELETE Statements
- •UPDATE Statements
- •SELECT Statements
- •Review
- •Quiz Yourself
- •The XML Design Specs
- •The Structure of XML Documents
- •XML Syntax
- •XML and the .NET Framework
- •Review
- •Quiz Yourself
- •ASP.NET Events
- •Page Directives
- •Namespaces
- •Choosing a Language
- •Review
- •Quiz Yourself
- •Introducing HTML Controls
- •Using HTML controls
- •How HTML controls work
- •Intrinsic HTML controls
- •HTML Control Events
- •The Page_OnLoad event
- •Custom event handlers
- •Review
- •Quiz Yourself
- •Intrinsic Controls
- •Using intrinsic controls
- •Handling intrinsic Web control events
- •List Controls
- •Rich Controls
- •Review
- •Quiz Yourself
- •Creating a User Control
- •Adding User Control Properties
- •Writing Custom Control Methods
- •Implementing User Control Events
- •Review
- •Quiz Yourself
- •Common Aspects of Validation Controls
- •Display property
- •Type Property
- •Operator Property
- •Using Validation Controls
- •RequiredFieldValidator
- •RegularExpressionValidator
- •CompareValidator
- •RangeValidator
- •CustomValidator
- •ValidationSummaryx
- •Review
- •Quiz Yourself
- •Maintaining State Out of Process for Scalability
- •No More Cookies but Plenty of Milk!
- •Out of Process State Management
- •Review
- •Quiz Yourself
- •Introducing the Key Security Mechanisms
- •Web.config and Security
- •Special identities
- •Using request types to limit access
- •New Tricks for Forms-based Authentication
- •Using the Passport Authentication Provider
- •Review
- •Quiz Yourself
- •ASP.NET Updates to the ASP Response Model
- •Caching with ASP.NET
- •Page Output Caching
- •Absolute cache expiration
- •Sliding cache expiration
- •Fragment Caching
- •Page Data Caching
- •Expiration
- •File and Key Dependency and Scavenging
- •Review
- •Quiz Yourself
- •A Brief History of Microsoft Data Access
- •Differences between ADO and ADO.NET
- •Transmission formats
- •Connected versus disconnected datasets
- •COM marshaling versus text-based data transmission
- •Variant versus strongly typed data
- •Data schema
- •ADO.NET Managed Provider Versus SQL Managed Provider
- •Review
- •Quiz Yourself
- •Review
- •Quiz Yourself
- •Creating a Connection
- •Opening a Connection
- •Using Transactions
- •Review
- •Quiz Yourself
- •Building a Command
- •Connection property
- •CommandText property
- •CommandType property
- •CommandTimeout property
- •Appending parameters
- •Executing a Command
- •ExecuteNonQuery method
- •Prepare method
- •ExecuteReader method
- •Review
- •Quiz Yourself
- •Introducing DataReaders
- •Using DataReader Properties
- •Item property
- •FieldCount property
- •IsClosed property
- •RecordsAffected property
- •Using DataReader Methods
- •Read method
- •GetValue method
- •Get[Data Type] methods
- •GetOrdinal method
- •GetName method
- •Close method
- •Review
- •Quiz Yourself
- •Constructing a DataAdapter Object
- •SelectCommand property
- •UpdateCommand, DeleteCommand, and InsertCommand properties
- •Fill method
- •Update method
- •Dispose method
- •Using DataSet Objects
- •DataSetName property
- •CaseSensitive property
- •Review
- •Quiz Yourself
- •Constructing a DataSet
- •Tables property
- •TablesCollection Object
- •Count property
- •Item property
- •Contains method
- •CanRemove method
- •Remove method
- •Add method
- •DataTable Objects
- •CaseSensitive property
- •ChildRelations property
- •Columns property
- •Constraints property
- •DataSet property
- •DefaultView property
- •ParentRelations property
- •PrimaryKey property
- •Rows property
- •Dispose method
- •NewRow method
- •Review
- •Quiz Yourself
- •What Is Data Binding?
- •Binding to Arrays and Extended Object Types
- •Binding to Database Data
- •Binding to XML
- •TreeView Control
- •Implement the TreeView server control
- •Review
- •Quiz Yourself
- •DataGrid Control Basics
- •Binding a set of data to a DataGrid control
- •Formatting the output of a DataGrid control
- •Master/Detail Relationships with the DataGrid Control
- •Populating the Master control
- •Filtering the detail listing
- •Review
- •QUIZ YOURSELF
- •Updating Your Data
- •Handling the OnEditCommand Event
- •Handling the OnCancelCommand Event
- •Handling the OnUpdateCommand Event
- •Checking that the user input has been validated
- •Executing the update process
- •Deleting Data with the OnDeleteCommand Event
- •Sorting Columns with the DataGrid Control
- •Review
- •Quiz Yourself
- •What Is Data Shaping?
- •Why Shape Your Data?
- •DataSet Object
- •Shaping Data with the Relations Method
- •Review
- •Quiz Yourself
- •OLEDBError Object Description
- •OLEDBError Object Properties
- •OLEDBError Object Methods
- •OLEDBException Properties
- •Writing Errors to the Event Log
- •Review
- •Quiz Yourself
- •Introducing SOAP
- •Accessing Remote Data with SOAP
- •SOAP Discovery (DISCO)
- •Web Service Description Language (WSDL)
- •Using SOAP with ASP.NET
- •Review
- •Quiz Yourself
- •Developing a Web Service
- •Consuming a Web Service
- •Review
- •Quiz Yourself
- •ASP and ASP.NET Compatibility
- •Scripting language limitations
- •Rendering HTML page elements
- •Using script blocks
- •Syntax differences and language modifications
- •Running ASP Pages under Microsoft.NET
- •Using VB6 Components with ASP.NET
- •Review
- •Quiz Yourself
- •Preparing a Migration Path
- •ADO and ADO.NET Compatibility
- •Running ADO under ASP.NET
- •Early Binding ADO COM Objects in ASP.NET
- •Review
- •Quiz Yourself
- •Answers to Part Reviews
- •Friday Evening Review Answers
- •Saturday Morning Review Answers
- •Saturday Afternoon Review Answers
- •Saturday Evening Review Answers
- •Sunday Morning Review Answers
- •Sunday Afternoon Review Answers
- •What’s on the CD-ROM
- •System Requirements
- •Using the CD with Windows
- •What’s on the CD
- •The Software Directory
- •Troubleshooting
- •ADO.NET Class Descriptions
- •Coding Differences in ASP and ASP.NET
- •Retrieving a Table from a Database
- •Displaying a Table from a Database
- •Variable Declarations
- •Statements
- •Comments
- •Indexed Property Access
- •Using Arrays
- •Initializing Variables
- •If Statements
- •Case Statements
- •For Loops
- •While Loops
- •String Concatenation
- •Error Handling
- •Conversion of Variable Types
- •Index
S E S S I O N
29
Migrating from ASP to ASP.NET
Session Checklist
Understanding the major changes in syntax and function for ASP.NET
Identifying major areas of risk in migrating your applications
Understanding how ASP and ASP.NET can coexist
This session provides an overview of the challenges you will face as you migrate your applications from ASP to ASP.NET.
ASP and ASP.NET Compatibility
After installing the .NET Framework in your existing ASP environment, you will find that you are able to continue running your ASP pages without modification. Additionally, as you begin to write your own ASP.NET pages you will find that they operate nicely side by side with your existing ASP pages. However, as you begin to migrate code from your ASP pages to ASP.NET you will find that there are at least four major areas of differences in how you coded in ASP versus how you will code in ASP.NET:
You can only script in one language per page or user control.
Page rendering your content through function calls is no longer supported.
You must define your functions as script blocks.
There are syntax differences and language modifications.
292 |
Sunday Afternoon |
Scripting language limitations
When creating ASP pages, developers will commonly use VBScript, JScript, PerlScript, or even Python. While the use of scripting languages is fine given their ease of use, they do have several disadvantages over compilable languages such as C#, VB .NET, or C++. The primary disadvantage is performance. Every time an ASP page is requested, the page is interpreted by the relevant scripting engine and cached to increase performance. This is done through an in-memory cache, so at any time only a relatively small number of ASP pages can be effectively cached. The second major disadvantage is that scripting languages are often designed for accomplishing procedurally oriented tasks in a quick and effective manner. This often means that they lack the full functionality and extensibility that a C# or VB .NET language would support.
In ASP.NET, pages are generated with languages such as VB .NET, C++, and C#. ASP.NET is not limited to these languages, however, as ASP.NET pages can be created with any compiler that can generate .NET Common Language Runtime (CLR)-compliant code. Additionally once this code is compiled, regardless of the language, it is stored as Microsoft Intermediate Language (MSIL) in the form of an executable (.exe) or dynamic-link library (.dll) file. When the code is executed on a client system, it undergoes a final round of Just In Time (JIT) compilation to transform it into machine-specific instructions. Theoretically, this means that there are no performance differences between language selections in terms of final delivered performance! Code written for the .NET platform runs under the control of the CLR. Language selection under ASP.NET is thus a lifestyle choice rather than a decision based upon expected performance, integration with the API, or other factors. All languages have access to and support the same base set of classes and functionality, and all execute to the same performance standards.
The CLR has been designed to replace the existing runtime layers of COM, Microsoft Transaction Services (MTS), and COM+.
Note
In ASP, you could readily mingle VBScriptand JScript-coded functions in the same page. In ASP.NET, VBScript has been replaced by VB .NET, which provides a range of syntax modifications that you will need to address. Although JScript is still supported, most JScript developers will prefer to use C#. So, from a core code migration perspective, you should examine any of your existing ASP pages that incorporate more than one language on the server side.
Note
While you can no longer mix languages on the server side, you can include a client-side scripting language that is interpreted by the client browser and is different from what is used on the server side. For instance, you could have all of your business logic written in VB .NET and compiled on the server side, and have client-side code written in JavaScript.
Session 29—Migrating From ASP to ASP.NET |
293 |
Rendering HTML page elements
In order to manipulate the display properties of an HTML control in ASP, you would intermingle scripting values with the HTML of the control to modify font sizes, colors, and other properties of the HTML element — the final code would then be rendered appropriately and sent to the end user’s browser. In ASP.NET, although you can render directly to the browser, you should focus instead on using object-oriented techniques to modify the property values and execute the methods of the ASP.NET server controls.
Listing 29-1 shows an example of an ASP page written to display a textbox with a black background, gray text, 20pt font, and a defined text value.
Listing 29-1 Using ASP to render an HTML control
<%@Language = “VBScript”%> <%
Sub DisplayTextBox(forecolor, backcolor, value, fontsize) Response.Write(“<input type=’text’ name=’txtDisplay’ value=’” & value & “‘“) Response.Write(“style=color:” & forecolor & “;background-color:”& backcolor &
“;font-size:” & fontsize & “pt;>”)
Response.Write(“</textbox>”) End Sub
%>
<HTML>
<BODY>
<%DisplayTextBox “gray”,”black”, “Hello World”,”20” %> </BODY>
</HTML>
The DisplayTextBox function is called after the <body> tag has been rendered, and the subroutine accepts the forecolor, backcolor, value, and fontsize attributes passed in the function call. The DisplayTextBox then effectively generates a series of strings that are subsequently rendered in the browser.
In ASP.NET, you will need to establish script blocks, and implement structured functions, which can then manipulate the control directly through the control’s inherent properties and methods. This is illustrated in Listing 29-2.
Listing 29-2 Using ASP.NET to render an HTML control
<HTML>
<%@ Page Language=”VB” Debug=”False” Trace=”False” %> <HEAD>
<SCRIPT LANGUAGE=”vb” RUNAT=”server”>
Sub Page_Load(sender as Object, e as EventArgs) Dim oColor as System.Drawing.Color
UpdateTextBox(oColor.gray,oColor.black,”Hello World”, 20) End Sub
Function UpdateTextBox(forecolor, backcolor, value, fontsize) Dim oColor as System.Drawing.Color
Continued
294 |
Sunday Afternoon |
Listing 29-2 |
Continued |
txtDisplay.Text = value txtDisplay.ForeColor = forecolor txtDisplay.BackColor = backcolor
txtDisplay.Font.Size = FontUnit.Point(fontsize) End Function
</SCRIPT>
</HEAD>
<BODY>
<FORM RUNAT=”server” ID=”Form1”>
<ASP:TEXTBOX ID=”txtDisplay” RUNAT=”server” /> </FORM>
</BODY>
</HTML>
In this example, the UpdateTextBox function is called each time the page is rendered and passes the requisite parameters similar to what occurred in the ASP example. The UpdateTextBox function, however, operates very differently — instead of the developer modifying the output stream rendered in the browser directly, the developer works with the properties and methods of the TextBox to obtain the desired results.
In both the ASP and the ASP.NET examples, the formatted output and the rendered HTML are nearly identical, both generating a similar looking HTML 3.2-compliant rendered output.
<input name=”txtDisplay” type=”text” value=”Hello World” id=”txtDisplay” style=”color:Gray;background-color:Black;font-size:20pt;” />
Using script blocks
In ASP.NET, there is a radical shift away from supporting the unstructured, interpreted script code that was typically written in procedural terms. In ASP, during each page execution, the code begins processing the first line of code and then works its way down. This encouraged ASP developers to intermingle HTML code with the scripting logic to produce a mess of spaghetti code without good separation of logic and presentation layers.
The impact of the move to script blocks means that the code segment shown in Listing 29-3 will no longer work.
Listing 29-3 Mixing VBScript and HTML in ASP
<%@Language = “VBScript”%> <HTML>
<HEAD>
<%Function CreateTable()%> <TABLE>
<%For x = 1 to 10%> <TR>
<TD>
<%Response.Write “Cell Number” & x%> </TD>