|
Active Server PagesHere are my notes and resources I have found useful when doing ASP programming. Let me know what you find helpful or missing. |
|
|
History of Active Server Pages (ASP's)ASP was first announced by Microsoft in November, 1996 (code named "Denali") and introduced March 97 to manage HTTP 1.0 as a part of Microsoft's Internet Information Server version 3 (code name “Gibraltar”). ASP 2.0 came with IIS 4.0 installed from the NT4 Option Pack as Personal Web Server (PWS) and also on CD 2 of Visual Studio 6. PWS can also be installed on Windows 95 from the setup.exe in Add-ons\pws folder from the Win 98 CD after the 986KB Feb 98 WinSock2 Upgrade for Windows 95. ASP 3.0 comes with IIS 5.0 shipped with Windows 2000. It introduces the Server.Transfer method (which redirects without a trip back to the client) and the Server.Execute method (which, unlike includes, parses code conditionally). IIS 5.1 is installed with Windows XP Professional (not the XP Home version). ASP.NET (pronounced "dot net") has a whole new architecture — the .NET Framework which references "code behind" program logic written in C Sharp or other language that compiles into MSIL. Because each file of a particular language is handled by that language's compiler, unlike ASP, languages cannot intermix in a single program file. This also requires ASP.NET to run on IIS server, and not under PWS. ASP.NET 2.0, released with Visual Studion 2005, introduces controls such as <asp:Login ASP.NET 3.5 was released with Visual Studio 2008. Microsoft's ASP.NET 4.0 preview contains the AJAX Template Library and the AJAX Client Library for ADO.NET Data Services. |
|
ASP Coding
When a client's Web/Internet browser clients requests a URL with an .asp file instead of an .htm, the ASP Interpreter (ASP.DLL) is invoked. MS .NET services also recognize .aspx files for processing only by ASP.NET servers. Unlike .htm files, the full http:// reference must be used to invoke ASP files through a web application server because the whole point of using ASP files is to dynamically generate Javascript and HTML code rather than serving up static .htm files. The ASP interpreter acts on blocks of code between delimiter tags <% and %> The first line of an ASP file specifies the syntax to be expected by the interpreter: <%@Language=VBScript%>
This is the default. Specify Language=JScript to use Javascript syntax between delimiter tags <% and %>
<%@Language=Javascript runat="server"%>
Non-default code scripts (such as JScript within VBScript file) run on the server are (on IIS 4.0) executed before default script code. An example of how an ASP.NET page begins:
<%@ Page Language="vb"
AutoEventWireup="false"
Codebehind="Webform1.aspx.vb"
Inherits="MyApp.WebForm1"
%>
The Page Language must be "vb" because to create this, in Microsoft Visual Studio 2004, open a new Visual Basic Project and select template ASP.NET Web Application. Resources for several projects are grouped into a Visual Studio Solution. ASP.NET header <% Explicit="true" takes the place of <% Option Explicit %> My sample "MySolution" has these resources:
|
Web Environment VariablesVariables available to each session object is defined with Session_OnStart and _OnEnd events. objRecordset.Fields("DATE") = Now() objRecordset.Fields("HTTP_USER_AGENT") = Request.ServerVariables("HTTP_USER_AGENT") objRecordset.Fields("HTTP_REFERER") = Request.ServerVariables("HTTP_REFERER") objRecordset.Fields("QUERY_STRING") = Request.ServerVariables("QUERY_STRING") objRecordset.Fields("REMOTE_ADDR") = Request.ServerVariables("REMOTE_ADDR") objRecordset.Fields("REMOTE_HOST") = Request.ServerVariables("REMOTE_HOST") objRecordset.Fields("SERVER_PORT") = Request.ServerVariables("SERVER_PORT") objRecordset.Fields("URL") = Request.ServerVariables("URL") CGI codingprint "QUERY_STRING ", $ENV{'REMOTE_ADDR'}, "\n"; These variables maintained internally by web application programs such as CGI, ASP, and JSP.
|
|
ASP Object Model
|
|
Includes
Use "include" preprocessor directives to dynamically add code from another file whenever it's invoked. The adovbs.inc is in most ASP pages to define common literals. Including other files makes it easier to keep common elements such as menu bars, copyright statements, etc. consistent across in all pages of a website by using a single source file for the contents. This is typically used to enforce login before users can access a page:
<!--#includesecuritylevel1required.asp"-->
' Define VBScript Constants: <!-- #include file="common.asp" --> If the file is not in the folder, specify instead
<!-- #include virtual="../lib/edits.asp" -->
|
For ASP Developers:
|
Error HandlingClick "Submit" without entering any data on and see a sample error page:
[Microsoft][ODBC Microsoft Access Driver] Field 'tblAdoAdd.FirstName' cannot be a zero-length string. Add this under the </HTML> in ASP pages to trap time-outs:
<%
Sub OnTranactionAbort() response.clear Response.Write "Script timed out." end sub %> | ASP for dummies.com by Bill Hatfield at edgequest.com |
Flow Control
|
Opening and Closing Databases1. Create (instantiate) an ADO Connection (cnn) object:
Dim cnnXXX
2. Open a database using a Connection String
set cnnXXX=Server.CreateObject("ADODB.Connection")
cnnXXX.ConnectionString = strConn
cnnXXX.Open 3. Create (instantiate) a Recordset (rst) to hold the output:
Dim rstXXX
Set rstXXX = Server.CreateObject("ADODB.Recordset") rstXXX.CursorLocation=3 rstXXX.CacheSize=10 4. Define a SQL dynamic query string, (which ends with a semicolon):
Dim strSQL
strSQL = "SELECT * FROM People;" 5. Execute the SQL query through the connection and into the recordset:
Set rstXXX = cnnXXX.Execute( strSQL )
7. Finally, close the object and set it to nothing:
cmmXXX.Close
set cnnXXX=Nothing |
The RecordSource property identifies the information managed by the data control. This could relate to all of the information in a single table, some of the information from a single table, or information stored in multiple tables in the data source. |
Doing Stuff With a RecordsetYou can perform insert, update, delete, or selection of data on a database. To save the file to XML format: rstXML.Save Server.MapPath("db_xml.xml"), adPersistXML
Using a Forward-Only Cursor on an ADO Database To use the oRS object's default method to return the value of a column named PeopleNameLast from the first row:
oRS.MoveFirst
Response.Write oRS("PeopleNameLast") You can also use method MoveLast for the last row or MoveNext. Note that cursors to simple recordsets cannot move backward (using MovePrevious) through the database. |
Enterprise Application Development Data Management with MS-Access Developing ASP Components by Shelley Powers (Beijing: Cambridge O'Reilly and Associates, 1999) ASP in a Nutshell: A Desktop Quick Reference by Weissinger, A. Keyton (Sebastopol, Calif., 1999) |
Resources |
Related:
| Your first name: Your family name: Your location (city, country): Your Email address: |
Top of Page
Thank you! |