Introduction to Active Server Pages

With Active Server Pages (ASP), a technology developed by Microsoft, you can design and program powerful and dynamic web applications. The current version of ASP is 3.0, and it is available with Internet Information Services (IIS) 5.0. Active Server Pages are text files with the .asp extension. These files contain standard HTML formatting tags and embedded scripting statements. Because the default scripting language for ASP is VBScript, a subset of Visual Basic and Visual Basic for Applications, you already have many of the skills required to web-enable your Access applications. In addition, the tools you need to make Access work with the intranet or Internet are within your reach. Although most of the examples in this chapter have been prepared using Microsoft Visual InterDev 6.0, you can use Windows Notepad or any other text editor to write your code. So, where do you start? You can start by acquiring some working knowledge of the HyperText Markup Language (HTML). A good place to start is the Internet. For easy, step-by-step tutorials, check out the following web sites: http://www.htmlgoodies.com or http://www.html-tutorials.ca/. Because Active Server Pages are a mix of HTML with a programming language such as VBScript or JavaScript, you should learn as much as you can about each component.

To better understand this chapter's topics, here are some terms to get acquainted with:

HyperText Markup Language (HTML) — A simple, text-based language that uses special commands known as tags to create a document that can be viewed in a browser. HTML tags begin with a less-than sign (<) and end with a greater-than sign (>). For example, to indicate that the text should be displayed in bold letters, you simply type your text between the <B> and </B> tags like this:

<B>This text will appear in bold letters</B>

Using plain HTML you can produce static web pages with text, images, and hyperlinks to other web pages.

Dynamic HTML (DHTML) — Allows the HTML tags to be changed programmatically via scripting. Use DHTML to add interactivity to your web pages.

VBScript — A scripting language based on Microsoft Visual Basic for Applications (VBA). Because this is just a subset ofVBA, some of the VBA features have been removed. For example, VBScript does not support data types — every variable is a variant. Like VBA, VBScript is an event-driven language — the VBScript code is executed in response to an event caused by a user action or the web browser itself. JavaScript — A compact, object-oriented scripting language invented by Netscape and used for developing client and server Internet applications. This is a cross-platform language that can be embedded in other products and applications, such as web browsers.

Taking Your VBA Programming Skills to the Web

Active Server Pages (ASP) — An Internet technology that enables you to combine HTML, scripts, and reusable ActiveX server components to create dynamic and powerful web-based applications and business solutions. ASP is not limited to a particular language. To create ASP pages, you can use VBScript, JavaScript, or any language for which you have a third-party ActiveX scripting engine.

While HTML pages store the actual data, Active Server Pages only store the information on how to obtain the data. How does this work? Suppose you typed the address of the ASP page in your web browser's address bar and pressed Enter or clicked the Go button. The web server will read the VBScript instructions contained in the ASP page and access the specified database. Once the data is obtained, the web server will put this information into an HTML page and return that page to you in the web browser in plain HTML code. Users never see the instructions contained in your ASP file unless they have access to the web server and have been given the appropriate permissions to open these files. You can rest assured that with ASP your development ideas and intellectual property are well protected.

Because the web server reads and processes the instructions in the ASP page every time your browser requests the page, the information you receive is highly dynamic. ASP allows the page to be built or customized on the fly before the page is returned to the browser. ASP is platform independent. This means that you can view ASP pages in any browser. The two most popular browsers are Internet Explorer and Netscape Navigator.

Hands-on 28-2 shows and describes the contents of the Employees.asp file you created earlier.

©Hands-On 28-2 : Viewing the Contents of an ASP Page

This hands-on uses the Employees.asp file that was created in Hands-On 28-1.

1. Click the Start menu button and choose Run. In the Run dialog box, type notepad C:\Learn_ASP\Employees.asp (substitute the correct path if necessary) and click OK to open the Employees.asp file. When the text editor opens, you will see the Active Server Pages code, as shown in Figure 28-3.

2. Scroll down in Notepad and take a look at the structure of this file. Notice the many tags between the angle brackets: <HTML>, <HEAD>, </HEAD>, <TITLE>, </TITLE>, <BODY>, <B>, </B>, <TR>, and so on.

The tags tell the browser how to display the file. The <HTML> tag at the beginning of the file tells the browser that what follows is an HTML document. The closing </HTML> tag at the end of the file tells the browser that the HTML document is completed. The closing tags are denoted by placing the forward slash before the tag name (for example, </TITLE>, </BODY>, </HTML>). Closing tags cancel the effect of the tag. Refer to the web sites mentioned at the beginning of this section to learn the meaning of all the HTML tags used in the Employees.asp file.

Part V

mmm t? Emptoyees.iisp - Notepad

FJe Edt format View Help

<META HTT P-EQUI v="colnt eijt-*Type" CONrr EMT="t#( /trtm 1; djjr 5 art =v1 ndows -II5 2 ">

crrrLEtEraplDyees-i/riTLE;-

if isob3ecti!5ess1onC"Northw1nii_conn")) Then set conn = sesslonC'Northwind-Conn")

Else set conn = server.createoMectC'AEQDe.connectlon") conn.open "Northwind","Attain","" set sesslonC'NorthvlncLconn") = conn

End if

if isobjectCsess-ionC'Employees.rs"}) Then set rs = sesslonC'Einployees^rs"}

Else sql - "select * from [Employees]" set rs = s erver. ere at eobjectC'ADODB. Recordset rs.open sql. conn, 3. 3 if rs.eof Then rs,AddN&v End if set sessionC'Employees.rs") = rs

End if

■iTABL E B0RDER=1 BGCOLOR =iHrfffff CELL SPAC I NG= 0 XFO NT" FACE="Arial" COLO R =£00 0000 iCAPT ION > <B? Empl OyetS -i/B;--i/CAPT I ON? </F0 WT--

Figure 28-3: You can explore the contents of the Employees.asp file created by the Microsoft Access Export command in Hands-On 28-1 by opening it in Notepad.

In addition to HTML tags that are interpreted by the browser when the page is viewed, the Employees.asp file contains the <% and %> symbols. The <% says that what follows is a server-side script, not HTML. The %> indicates the end of a script segment. The script code between the <% and %> delimiters is executed on the web server as the page is processed. Any values you want returned by the script are placed between the <%= and %> delimiters.

Active Server Pages (ASP) is a form of server-side scripting. A server-side script is the script code that runs on the web server before the page comes down to the client (the user's machine). This script begins to run when a browser requests an .asp file from your web server. The web server then calls the ASP Interpreter (ASP.dll), which processes the blocks of code between the <% and %> delimiter tags. After the script commands are executed, the web page is sent to the browser. Server-side scripts cannot be readily copied because only the result of the script is returned to the browser. Users cannot view the script commands that created the page they are viewing. All they can see is the HTML source code for the page.

In addition to server-side scripts, an .asp file can contain client scripts. A client script is the script code that is processed by a browser on the user's machine while the page is viewed. Client scripts are enclosed between <SCRIPT> and </SCRIPT> tags. When a browser encounters a <SCRIPT> tag, it sends the script that follows this tag to a scripting engine. A scripting engine is the part of the web browser that processes the scripts. Because not all browsers can process client scripts, comment tags (<!-- and -->) are often used to make browsers that do not recognize the <SCRIPT> tag ignore it.

Taking Your VBA Programming Skills to the Web

When you scroll down in the Employees.asp file, you will notice several VBScript lines that output data to a web page. The output appears between the <%= and %> delimiters (notice the highlighted statement in Figure 28-4).

r Employees.asp - Notepad

File Edit Fccmat View

On Error Resine Next rs.MoveFirst do while Not rs.eof

<TR VALIGN=TOP> <TD BOR DERCOLOR ÚcOc 0 COLO R=#0000 Q0>| <TD BORDERCOLOR=#C0COCO coLOR=#OOQOOOx£=5erver, <TD BORDERCOLOR=#C0CQC0 COLO R=#0 000 0qx%=Se r ve r, <TD BORDERCOLOR=#c0COCO

<td bor d ercolor =#c 0 cOc 0 colo r=#0 000 0üx3£=Se r v e r, <td bordercolor=#cOcOcÜ colo r-#OOOOOOxS¿wSer ver, <td bordercolor-^cococo colo r-#0 000 00> <%'Se rve r. <td bordercolor»^cococo colo r-(^0000 OGxSé^Se r ve r, <td bordercolor=#c0c0c0 colo r=#OOOOOOx3S=Ser ver. <td bor d ercolor =#c 0 cOc 0 colo r=#000000x%=5er ver,

<td bordercolor=#cococo colo r=#000000x?£=5er ver, <td bordercolor=#c0coc0 colo r=#oooo OOxX=se r ve r , <td bordercolor=#c0coc0 colo r=#0 000 0úx3£=Ser ver. <td bordercolor=£cüc0c0 colo rw#OOOOÜOx5&*Ser ver. <td bordercolor-£c0c0c0 colo r-#OOOOOOx9é-Ser ver , <td bor dercolor0 cOc 0 color-#000000x%"Server. <td bordercolor=fc0c0c0 colo r-#OOOOOGx36=Ser ver.

FACE="Arial"

XFONT Style=FONT-5IZE:1Opt FACE = "Arial " htmle n code (r s, f i e 1 ds (" La s t Nam e " ), va 1ue)a>>< b r ></font ></td> xfont si:yle=FOMT-5iZE:10pr FACE="Ar1a"l" html E n code (r s. f1 e 1 ds ("' r s z N ame"), va 1 ue ) %>< brx/font>< /to> XFONT Style=F0NT-SI2E :10pt FACE = "Ar1a1" htmle n cod e (r s. F1 e 1 ds ("' t 111 e'"), va 1 ue) 3£>< b rx /font><Ad>

XFONT Style=FONT-SIZE:l0pt FACE = "ar1a"l"

htmle n cod e (r s- Fi e 1 ds ( " Ti 11 eof Cou r t es y" ).. Val ue}S£xBR x/FONT > </T D>

ALIGN =RIGHTxFONT Styl e- FONT -SIZE :1 Opt FACE="Arial" HTMLEncode(rs. Fi el ds ( Bi rthDate"). val ue)s£xbrx/FONTxAo> ALIGN-RIG HTXFONT styl e- FONT -size :1 Opt FACE-"Arial"' HTMLE n code (r s. Fi e 1 ds ( Hi r eDat e " ). Va 1 ue)£xB Rx/font > </T D> xfont style-font-size:10pt face-"Arial" HTMLEncode(rs. Fi el ds ("Address "). val ue)%xBRx/FONTxAD>-xfont style=font-siZE:10pt face="aria"l" HTMLEncode (r s. Fi e 1 ds ( "ci t y" ). VaT ue)a>xBR x/FONT xA D> XFONT Style=FONT-5IZE:10pt FACE = "AriaT' htmle n code O s, Fi e 1 ds (" r eg i on"). val ue )%x br> </fo ntx/td >

XFONT Style=FONT-5lZE:10pt FACE = "Ar1a1"

HTMLEncode(rs, f1 el ds ("postalcode . val ue)%xeRX/FONTxA0>

xfont style=font-size:! opt: face="ar1a1"

htmle n code (r s > f1 e 1 ds ("Co u nt r y" ). val u e')%> <6R > </font > </t d >

XFONT Style=FONT-SIZE:lOpt FACE = "Ar 1 al"

htmle n cod e (r s. f1 e 1 ds ( " Hon ePh one " ), va 1 ue ) 5£x BRx/FONTx /TD> xfont Style=font-SI2E:!Opt face = "Ar i al" htmle n cod e (r s > Fi e 1 ds ( " Ext ens i on " ). va 1 ue) %>< BRx/font></rb> xfont styl e-font-size :10pt f^ce-"arial" HTMLEn cod e (r s. Fi e 1 ds ( " Ph ot o'"). va 1 ue) B Rx /fontxA*» xfont style-f

-font-size:10pt face-"arial"

HTMLEn code (r s. Fi el ds ("Not es '"). val ue)s6><brx/FQNTx/td> XFONT Style=FONT-SIZE:10pt face = "Ar i al "

HTMLEn cod e (r s. Fi e 1 ds ( " Re p ort s To " ). Va 1 ue ) %>< BRx/FONTx/td>

Figure 28-4: The contents of an ASP page as displayed in the Windows Notepad application. The highlighted statement will cause the value of the Employee field to appear on the web page when the page is viewed in the browser.

By using the equals sign inside these delimiters, you can write a VBScript variable value to a web page.

3. Close the Employees.asp file and exit Notepad.

The ASP Object Model

ASP has its own object model consisting of the objects shown in Table 28-1. Table 28-1: The ASP Object Model

ASP Object Name

Object Description

Request

Obtains information from a user

Response

Sends the information to the client browser

Application

Shares information for all the users of an application

Server

Creates server components and server settings

Session

Stores information pertaining to a particular visitor

Part V

The ASP objects have methods, properties, and events that can be called to manipulate various features. For example, the Response object's Write method allows you to write text to the client browser. The CreateObject method of the Server object is required to create a link between a web page and your Access database. Although this chapter does not introduce you to all of the available ASP objects, properties, methods, and events, you will learn how to use the objects and methods required for performing a specific task related to displaying or manipulating Access data from the web browser.

+1 0

Post a comment