In a World Of Databases, ASP and Text Files Page 2

By ServerWatch Staff (Send Email)
Posted Jan 24, 1999


Fast fill-ups, ASP and Text Files

The select box.
Putting dynamic data into select boxes at runtime is very similar to the previous example. But this time a Dictionary object is created to focus on a likely scenario in production. The reason is the Dictionary object allows some functions which are very useful by the fact they emulate a database. Other pieces are the page header and body containing the form, and a footer. Many of these pieces are typically server side includes as well, but to illustrate the versatility of the text file, I'll use text files instead of includes for the header and footer for this sample code.

The key to it all
An easy way to create useful data is to organize it in key/value pairs. ASP's Dictionary object's properties are CompareMode, Count, Item and Key, and the methods are Add, Remove, RemoveAll, Items, Keys, and Exists.

Using key=URL and value=topic
Let's presume to have a select box with links to other pages on the site and the topics of each. We'll want to display the topic and use the URL to redirect on the choice of the user. Both of these are stored as key/value pairs. The construction of the object is to set the properties and then execute the creation of the object.

There are rules to the game which help control data quality. Data errors are produced by two conditions: If a key already exists when adding; or, a key isn't there when removing. Three compare modes are possible: vbBinaryCompare, vbTextCompare and vbDatabaseCompare. These are handy.

Creating the Object
In this case, typical code to create the Dictionary object would look like:

dim dicKey, dicVal, dicObj
set dicObj = createobject("Scripting.Dictionary")

So, the next step is to create a file system object and open it to get the data for the dictionary.

dim navfile, fileObj
set fileObj = createobject("Scripting.FileSystemObject")
Did the object get created? If yes, open the file:

if isobject(fileObj) then
set navfile = fileObj.opentextfile("D:\webshare\wwwroot\asp\nav2.txt")
As before, an error can occur here if the file isn't found, or can't be opened by the system so having an 'on error' statement is important to keep the client application from hanging. To handle this error one could use a default list in memory which was included at runtime, or hard-coding URL's which are used on file system errors (404's). The error code to create a default dictionary would look something like:
on error resume next
dicObj.item("default.htm") = "Industrial Design and Other Interests"
dicObj.item("products.htm") = "An Interesting Array of Products"
dicObj.item("prices.htm") = "Current Pricing"
dicObj.item("technical.htm") = "Technical Details of All Products"
dicObj.item("white_papers.htm") = "White Papers Outlining Features and Scope"
dicObj.item("support.htm") = "Contact Our Friendly Support Crew"
dicObj.item("archive.htm") = "Dive Into the Archives for Facts"

The code now has nav2.txt open in read-only mode if there wasn't an error. To read a line you code a statement:

while not navfile.atendofline
dim nav
nav = navfile.readline

We need to separate this string which is separated by a comma. The instr() and mid() functions are the basic tools of parsing strings. Instr() is used to find a place, and mid() is used to retrieve it. With two variables, two mid()'s will retreive the key and value. This may seem like a lot of work...
dim comma, start, length, aurl, atopic
start = instr(nav, comma)
length = len(nav)
aurl = mid(1, start)
atopic = mid(start+1,length)
Now all the pieces are ready to build the dynamic part of the coding. This example continues the navigation theme, but it can be useful for many other items, especially graphic libraries. The number of columns is one of the limiting factors in using text based data. Knowing this, it's still common to be able to create data structures with two to five columns.
response.write("<option value=' " & aurl & " '>" & atopic & vbCrLf)

The next part is consideration of how to build the client page. If we want to use this box to redirect, it's handy to store the url's as option values and then use those on the client with javascript to redirect without using another server hit. The code above lists the option part of the select box, the javascript is hard-coded into the response writes. Note the single quotes within these write statements for normally double quoted attributes.

<%@ Language="VBScript"%>
<% response.buffer = True%>
response.write("<!-- DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'-->" & vbCrLf)
response.write("<html>" & vbCrLf)
response.write("<head>" & vbCrLf)
response.write("<title>Fill Those Select Boxes</title>" & vbCrLf)
response.write("<script language='JavaScript'>" & vbCrLf)
response.write("<!--" & vbCrLf)
response.write("function gothere(){" & vbCrLf)
response.write("var aurl = nav.nav.value" & vbCrLf)
response.write("location.href = aurl" & vbCrLf)
response.write("}" & vbCrLf)
response.write("//-->" & vbCrLf)
response.write("</script>" & vbCrLf)
response.write("</head>" & vbCrLf)
response.write("<body bgcolor=ffffff link=0000ff vlink=8e2323 alink=00009c>" & vbCrLf)
response.write("<basefont face='verdana,arial,helvetica' size=2 color=000000>" & vbCrLf)
response.write("<form name='nav'>" & vbCrLf)
response.write("<font size=6 color=800000 face='Arial,Helvetica,Verdana'></font><br>" & vbCrLf)
response.write("<table border='1' cellspacing='2' cellpadding='12'><tr><td>" & vbCrLf)
response.write("<select name='nav'>" & vbCrLf)
dim dicKey, dicVal, dicObj
set dicObj = createobject("Scripting.Dictionary")
dim navfile, fileObj
set fileObj = createobject("Scripting.FileSystemObject")
if isobject(fileObj) then
set navfile = fileObj.opentextfile("D:\webshare\wwwroot\asp\nav2.txt")
end if
while not navfile.atendofline
dim nav
dim comma, start, length, aurl, atopic
comma = chr(44)
nav = navfile.readline
start = instr(nav, comma)
length = len(nav)
aurl = mid(nav, 1, start-1)
atopic = mid(nav, start+1)
response.write("<option value=chr()" & aurl & "'>" & atopic & "<br>")
length = 0
start = 0
response.write("<input type='Button' value='GO' onclick='gothere();' style='font-weight: bold; font-family: arialblack; color: white; background-color: Red; padding: 3;'></td></tr></table>")
set fileObj = nothing
What this code does
It creates a select box for navigation. The displayed list is from a text file with two columns separated by a comma. The page uses client side javascript to send the user to the topic selected. The coding uses a Dictionary object to store the URL and topic description, the next article shows how to use this object for database emulation of larger text files and arrays.

Page 2 of 3

Comment and Contribute

Your name/nickname

Your email

(Maximum characters: 1200). You have characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date