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 populate a default dictionary would look something like below, yet I'll leave it out for brevity from the finished coding:
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...
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.buffer = True%>
<% 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
nav = navfile.readline
start =instr(nav, comma)
aurl =mid(nav, 1, start-1)
atopic =mid(nav, start+1) response.write("<option value=chr()" & aurl & "'>" & atopic & "<br>")
start =0 wend response.write("</select></td><td>") 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>") response.write("</form>") response.write("</body>") response.write("</html>")
navfile.close set fileObj = nothing %>
What this code does
Double quotes separated from single quotes for clarity, don't do this !
Previous versions of VBScript are CaseSensitive, this is written for the latest version.
Development Test System: Windows98, PentiumII 200MHz 128Mb
Personal Web Server 4.0 (IIS4), SQL7b3, Personal Oracle, Sybase