ASP - Active Server Pages Tutorial - Visual Basic Explorer

What is ASP?

By Anne-Marie Wright

This tutorial assumes a basic knowledge of HTML

ASP - Active Server Page is a scripted page where the script code is run on the web server and plain HTML is sent to be viewed locally on the users browser. This is useful because the programming code is not sent to the browser, so the users cannot view it using the 'view code' option. Also you can return different pages to different users based on their browser types, stored information regarding the users preferences, their 'security' level or other dynamic options you program into your pages. One of the most common uses of ASP is displaying database information. Since this code is first processed on the server and then returned as plain HTML even browsers that aren't Java, Script or ActiveX enabled can see your page since what they are getting is HTML.

Note: To run the examples in this tutorial you will need to have a web server. Microsoft has Personal Web Server (PWS) which can be installed from the Internet Explorer, VB distribution disks or the Windows 98 CD. If you need help setting up PWS please ask on the forums page. Let's have a look at a small example of a Active Server Page. 


<HTML>

<HEAD>

  <TITLE>Hello World</TITLE>

</HEAD>

<BODY>

<%

  Dim TimeNow

  Dim DateNow



  TimeNow=Time

  DateNow=Date

%>

<H1>Hello World</H1>

<P>The Date here is <%=DateNow%></P>

<P>and the Time is <%=TimeNow%></P>

</BODY>

</HTML>

This is a simple web page, when called the page would look like:

Hello World
The Date here is 12/07/00
and the Time is 12:00:34

If you have a look at the source code that the browser has received, you will notice that only the HTML has been sent - no code. Lets now look at how this was achieved The first part of the document is normal HTML...


<HTML>

<HEAD>

  <TITLE>Hello World</TITLE>

</HEAD>

<BODY>

The next part of the document is the server side code...


<%

  Dim TimeNow

  Dim DateNow



  TimeNow=Time

  DateNow=Date

%>

<% is the tag that shows the start of the server side code and %> shows the end of the code block. When someone requests a page with the .asp extension (it is possible to set up the server to parse other extensions for asp code) the server looks at the page, finds any code blocks enclosed by <% and %> tags, processes the code and in this example inserts the resultant HTML into the document. 

All variables in script are variants. It is not necessary to declare the variables unless you use option explicit but it is good programming practice and recommended that you do so.

Back to our example...the time and date are then stored in the variables to be called later on. We mentioned that %> is the tag that shows the end of the server side code. This next part of the code is again mostly normal html...


<H1>Hello World</H1>

<P>The Date here is <%=DateNow%></P>

<P>and the Time is <%=TimeNow%></P>

</BODY>

</HTML>

<%=DateNow%> displays the contents of the variable. We could have also done <%=Date%> instead.

This example doesn't do much but should begin to give you an idea of how useful ASP can be in creating dynamic and interactive web applications. You should also have been pleasantly surprised to note the similarity in syntax to the Visual Basic we all know and love which could offer you the opportunity to add another useful programming skill to your tool belt. Now let's look at transferring information between pages which is something you are likely to find more useful.

There are several ways of transferring information between pages the URL,  Cookies, Session variables and forms.This next example uses a form to transfer information This file is just a normal HTM file there is no code in it...

'---name.htm---


<HTML>

<HEAD>

  <TITLE>User Information</TITLE>

</HEAD>

<BODY>

  <FORM Method=post Action=result.asp>

  <BR>Name: <INPUT Type="text" Name="Name"

size=20>

  <BR>Address: <INPUT Type="text"

Name="Address" size=50>

  <br><INPUT Type="submit" Name="B1"

value="Submit">

  </FORM>

</BODY>

When the Submit button is pressed then it will call the file result.asp...

'---result.asp---


<HTML>

<HEAD>

  <TITLE>User information - Results</TITLE>

</HEAD>

<BODY>

<h1>User Information - Result</h1>

<%

  dim Name

  dim Address



  Name=request.form("Name")

  Address=request.form("Address")

%>



You have entered the following information:



<br>Name is <%=Name%>

<br>At Address <%=Address%>

</BODY>

So now lets look at what happens here. The name.htm file is the first page that you see, the user would fill out the text boxes and when they are ready to proceed will click the submit button. The action on the submit button is to run the result.asp page. Result.asp gets the information from each of the text boxes on the form in name.htm and saves them in variables on the page. Then the information is displayed later in the HTML.

Another way to exchange information between pages is to use the URL. This next example uses the URL to pass a database id to the next page. Here we have a simple database with one table that contains information about a companies products. Initially we will show all of the available products by name and if the user wants more information they can click on the name which will take us to another page that displays the whole information about the product.

'---db.asp---


<%



Dim conn

Dim tb

Dim TotalRec



sub ConnectDB(DSName)



  set conn = server.createobject("adodb.connection")



  conn.open DSName



end sub





function RunSQL(sql)



  set tb = server.createobject("adodb.recordset")

  tb.open sql, conn, 3, 3

  if tb.eof then

    Runsql = false

  else

    RunSQL = True

  end if

end function





sub Closetb



   tb.close

   set tb = nothing

end sub





sub CloseDB



   conn.close

   set conn = nothing

end sub

%>

This first file holds reusable code. The above functions would be used in most database handling situations to do with the web. Rather than having the code in every page it is possible to create the functions in another file and add a reference to it in all the pages that need to access a database. I'll show you how to do that with an include statement -or Server-Side Include (SSI)- after this next code block.

The variable declaration works the same as in VB. If the variables are declared outside the function they are global to the page, if they are inside the function they are local to the function. Let look at each of the functions...


sub ConnectDB(DSName)

  set conn = server.createobject("adodb.connection")

  conn.open DSName

end sub

This function creates and opens an ado connection to the database.


function RunSQL(sql)

  set tb = server.createobject("adodb.recordset")

  tb.open sql, conn, 3, 3

  if tb.eof then

    Runsql = false

  else

    RunSQL = True

  end if

end function

This function creates an ADO recordset. Then runs the select SQL sent as a parameter. If there are no records returned by the sql then the RunSQL returns a false, otherwise it returns a true...


 sub Closetb

   tb.close

   set tb = nothing

end sub

sub CloseDB

   conn.close

   set conn = nothing

end sub

The two close functions are tidy up functions. It is good practice to clear objects when we are finished with them.

'---widget.asp---


<html>

<head>

<title>Widgetopia Inc.</title>

</head>



<body bgcolor="#C0C0C0">



<h1 align="center"><font size="5">Widgetopia Inc.</font></h1>
<!--#include virtual="db.asp"-->
<p>Please select the product from list below for further

information.</p>
<div align="center">

  <table border="1" cellpadding="0" cellspacing="0"

width="60%">

    <tr>

      <td>

        <p align="center"><font size="3">Products</font></p>

      </td>

      <td>Price</td>

    </tr>

<%

  Dim Product

  Dim Price

  Dim ProductId



  ConnectDB "DSN=Widget"

  strSQL="SELECT * FROM Items"

  if RunSQL(strSQL)=False then

    response.write "<center>There are no records in

the database"

    response.write "<br>Please check back

later</center>"

    response.end

  else

    Do while not tb.eof

       Product=tb("Description")

       Price=FormatCurrency(tb("Price"))

       ProductId=tb("WidgetId")

%>

    <tr>

      <td><a href="items.asp?Id=<%=ProductId%>"><%=Product%></a></td>



      <td><%=Price%>

    </tr>

<%

      tb.movenext

    Loop

%>

  </table>

<%

    Closetb

  End if

  CloseDB

%>

</div>

</body>

</html>

This page displays all the available products and their prices. This next line shows how the global function file db.asp is included in this page via a server side include statement.

<!--#include virtual="db.asp"-->

Note: Server-side includes are parsed by the server before ASP script so you can't use an IF...Then statement for example to insert different include files based on a condition -as nice as that would be. There are some alternatives if you need this functionality but they are beyond the scope of this current tutorial. Feel free to stop by the forums if you need help doing something like this.

The code connects to the database and opens the table so the information can be displayed. If no information is returned then the page would display the message in the response.write statements. The Response object is used to send information to the clients browser. It has several properties and methods which will set it up.

The write method - writes a variable to the current HTTP output as a string.

The End method - stops processing the .asp file and returns the current result.

The FormatCurrency function returns the information formatted as a currency value using the currency symbol defined in the system control panel.

To process the information the do loop works the same as in VB. The result rows in the table are shown in this example as html code, there is no reason why we could not have used the response object to write all the information to the browser.

<td><a href="items.asp?Id=<%=ProductId%>"><%=Product%></a></td>

This line sets up the link to the next page. Notice that the page name is followed by ?ID=x 

? denotes that it is a querystring

ID is the name of the identifier

x is the result of the identifier

When the user clicks on a link the items.asp page is run.

'---items.asp---


<html>

<head>

<title>Widgetopia Inc.</title>

</head>



<body bgcolor="#C0C0C0">



<h1 align="center"><font size="5">Widgetopia

Inc.</font></h1>

<!--#include virtual="db.asp"-->

<%

  Id=request.QueryString("ID")

  connectDb "DSN=Widget"

  strSql="SELECT * FROM Items WHERE WidgetId=" & ID

  if RunSQL(strSQL)=False then

    response.write "<center>There are no records in

the database"

    response.write "<br>Please check back

later</center>"

    response.end

  else

%>

<div align="center">

  <table border="0" cellpadding="0" cellspacing="0"

width="60%">

    <tr>

      <td>

        <b>Description </b><%=tb("Description")%>

      </td>

    </tr>

    <tr>

      <td>

        <img src=\Widget\<%=tb("Picture")%>>

      </td>

      <td>

        <b>Price </b><%=FormatCurrency(tb("Price"))%>

      </td>

    </tr>

    <tr>

      <td>

       

<b>Comments</b><p><%=tb("Comments")%></p>

      </td>

    </tr>

  </table>

<%

    Closetb

  End if

  CloseDB

%>

<br>

<table width=20%>

  <tr>

    <td><a href=widget.asp>Return to Menu</a></td>

  </tr>

</table>

</div>

</body>

</html>

As we are taking to the database, the db.asp is reference again. Id=request.QueryString("ID") gets the information from the URL that was passed from the form and stores it in the pages Id variable. The rest is similar to the previous page. This is a brief look at what it is possible to do with active server pages. There is a lot more to it, we have not looked at cookies or session variables and there is a lot of new and exciting 'stuff' on the ASP horizon but I hope this gives you some idea of what is possible and whets your desire to try out some ASP programming.



Close    To Top
  • Prev Article-Programming:
  • Next Article-Programming:
  • Now: Tutorial for Web and Software Design > Programming > VB > Programming Content
    Photoshop Tutorial
     

    Special Effect

      3D Effect
      Photoshop Articles
    Programming Tutorial
     

    C/C++ Tutorial

      Visual Basic
      C# Tutorial
    Database Tutorial
     

    MySQL Tutorial

      MS SQL Tutorial
      Oracle Tutorial
    Geek Tutorial
     

    Blogging Tutorial

      RSS Tutorial
      Podcasting Tutorial
    Graphic Design Tutorial
      Coreldraw Tutorial
      Illustrator Tutorial
      3D Tutorials
    Webmaster Articles
     

    Domain Service

      Web Hosting
      Site Promotion
    Java Tutorial/ Articles
     

    Java Servlets

      JavaEE Tutorial
     

    JavaBeans Tutorial

    XML Tutorial/ Articles
     

    XML Style

      AJAX Tutorial
      XML Mobile
    Flash Tutorial/ Articles
     

    Flash Video

      Action Script
      Flash Articles
    OS Tutorial/ Articles
      Linux Tutorial
      Symbian Tutorial
      MacOS Tutorial
    Personal Tech
      Hardware Tutorial
      Software Tutorial
      Online Auction