dcsimg

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

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


T.Mallard

Dyno default.asp, ASP and Text Files

So now most all of the default.asp homepage has been assembled by opening simple text files and filling the areas with the text file content. Suddenly this whole page can be left alone for long periods of time. If you make a mistake in the text file, this normally won't affect operations at all. Your customers may think twice...oooops.

The homeboy.

Ok, there's no browser checking, just a little code for noframes, this is very basic. Most developers of websites must be concerned with the user viewpoint and supporting the content for a variety of browsers is a fairly common procedure. There are two strategies for this, send javascript to the client and document.write at the places on a page where browser detection is needed; or, detect from the HTTP headers what browser is making the request and send a static page.

What the overall design intends to do has a great influence on the presentation of the page. Design elements in this case were composed of frames. The text file system can also produce composite single pages with this same source material. This would avoid the frames issue entirely by producing a page with tables instead.

To begin, the code for the banner is simple, just some font tags.

<div align="center"><b><font face="Book Antiqua" size=4><font size=6>O</font>nline<font size=6>A</font>rticles and <font size=6>P</font>hotography</font></b></div>

In this example the main page is constructed in pieces as before. The graphic used is the ASP ad rotator component, the text is in a separate text file with the footer table also a separate file, and all this will now be used to produce a set of tables to replicate the look of frames. The tables only here allow the content to be seen by almost any browser, which is best for a homepage.

The code

Creating a default.asp
In this case, typical code to create the frames version of the page:

<!-- DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'-->
<html>
<head>
<title>Outdoor Photography and Other Interests</title>
</head>
<frameset rows="44,*" frameborder="yes" framespacing="1" border="1">
<frame name="banner" src="banner_01.asp" marginwidth="3" marginheight="3" scrolling="auto" frameborder="no">
<frameset cols="106,*">
<frame name="nav" src="nav_01.asp" marginwidth="3" marginheight="2"
scrolling="auto" frameborder="no">

<frame name="main" src="main_01.asp" marginwidth="6" marginheight="6"
scrolling="auto"frameborder="no">

</frameset>
<noframes>
<body>
Your browser does not support frames so won't be able to view this website, please download an up to date browser as no text-only navigation is available.
</body>
</noframes>
</frameset>
</html>
Examining the main page, it has a content text file, then the ad rotator and finally the footer is added from another text file.
dim main_obj, main_file, main_content
set obj_ad = server.createobject("Scripting.FileSystemObject")
set ad_file = obj_ad.opentextfile("D:/webshare/wwwroot/asp/examples/main_content.txt")
on error resume next
main_content = ad_file.readall

This has the file main_content.txt open and moved into the variable main_content. Next I'll insert the ad rotator which is supported by two other test files!! These control what images are displayed and the URL's for links. The first of these is called the schedule file as it defines the amount of relative time each ad gets, as well as the graphic size and sources:

REDIRECT ad_urls.asp
WIDTH 96
HEIGHT 64
BORDER 1
*
/gifs/xcntry_01.gif
http://tommalla/asp/examples/default_4.asp
Online articles and photography
2
/gifs/xcntry_02.gif
http://tommalla/asp/examples/default_4.asp
Other online articles and photography
2
As you can
dim mainfile, fileObj
set fileObj = createobject("Scripting.FileSystemObject")
Did the object get created? If yes, open the file:

if isobject(fileObj) then
set mainfile = fileObj.opentextfile("D:\webshare\wwwroot\asp\main_content.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 main_content.txt open in read-only mode if there wasn't an error. To read a line you code a statement:

while not mainfile.atendofline
dim main
main = mainfile.readline

Again we need to separate this string which is separated by a comma. 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.
dim comma, start, length, aurl, atopic
start = instr(main, comma)
length = len(main)
aurl = mid(1, start)
atopic = mid(start+1,length)
Next, main_01.asp uses the ad rotator (which only rotates on refresh).
dim obj_ads, ad_border, ad_click, ad_content2
set obj_ads = server.createobject("MSWC.AdRotator")
obj_ads.Border = (1)
obj_ads.Clickable = (True)
obj_ads.TargetFrame = ("_top")
ad_content2 = obj_ads.GetAdvertisement("ad_content.txt")
response.write(ad_content2)
For redirecting the urls, this coding is used.
dim gothere
gothere = request.querystring("url")
response.redirect(gothere)
Then there is the footer, stolen from another text file of course.

dim ad_obj, ad_file, ad_content
set obj_ad = server.createobject("Scripting.FileSystemObject")
set ad_file = obj_ad.opentextfile("D:/webshare/wwwroot/asp/examples/footer_01.txt")
on error resume next
ad_content = ad_file.readall
response.write(ad_content)
It looks like all the pieces are ready to build the dynamic part of the coding now. I'll have to create a master table and plot layouts to get a similar design to the frames version. This is nested tables, with the choice of either specifying percentages or absolute size in pixels as the usual methods of doing this. The outer table is a 100% wrapper with the first row the banner.
<table width=640 border=1 cellspacing=2 cellpadding=6>
<tr><td colspan=2 align="center" valign="middle"
bgcolor="White">banner</td></tr>
<tr><td width=48 rowspan=3 align="center" valign="top" bgcolor="White">nav</td><td width=592>main</td></tr>
<tr><td>rotator</td></tr>
<tr><td>footer</td></tr>
</table>
Seems to look about right. So, to assemble the page dynamically, these pieces will flow together as below.
<%@ Language="VBScript"%>
<% response.buffer = True%>
<%
response.write("<!-- DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 3.2 Final//EN'-->")
response.write("<html>")
response.write("<head><title>Website Navigation Page</title>")
response.write("</head>")
response.write("<body>")
response.write("<font face='verdana,arial,helvetica' size=2>")
response.write("<table width=640 border=1 cellspacing=0 cellpadding=1>")

dim abanner, bannertext, fileObj
set fileObj = createobject("Scripting.FileSystemObject")
if isobject(fileObj) then
set abanner = fileObj.opentextfile("D:\webshare\wwwroot\asp\examples\banner.txt")
bannertext = abanner.readall
end if
response.write("<tr><td border=1 colspan=2 align=center valign=middle bgcolor=ffffff>" & bannertext & "</td></tr>")
abanner.close
set fileObj = nothing
%>
<%
response.write("<tr><td width=48 rowspan=3 align=center valign=top bgcolor=ffffff>")
dim objfile, navfile, navout
set objfile = createobject("Scripting.FileSystemObject")
if isobject(objfile) then
set navfile = objfile.opentextfile("D:\webshare\wwwroot\asp\examples\nav.txt")
end if
while not navfile.atendofline
dim nav
nav = navfile.readline
navout = navout & "<tr><td bgcolor=ccffcc height=24>" & nav & "</td></tr>"
wend
navtable = "<table>" & navout & "</table>"
response.write(navtable & "</td><td width=592>")
%>
<%
dim main_obj, main_file, main_content
set main_obj = server.createobject("Scripting.FileSystemObject")
set main_file = main_obj.opentextfile("D:/webshare/wwwroot/asp/examples/main_content.txt")
main_content = main_file.readall

dim obj_ads, ad_border, ad_click, ad_content
set obj_ads = server.createobject("MSWC.AdRotator")
obj_ads.Border = (1)
obj_ads.Clickable = (True)
obj_ads.TargetFrame = ("_top")
ad_content = obj_ads.GetAdvertisement("ad_content.txt")

dim foot_obj, footer_file, footer
set foot_obj = server.createobject("Scripting.FileSystemObject")
set footer_file = foot_obj.opentextfile("D:/webshare/wwwroot/asp/examples/footer_01.txt")
footer = footer_file.readall

mainout = "<table><tr><td>" & main_content & "</td></tr><tr><td>" & ad_content & "</td></tr><tr><td>" & footer & "</td></tr></table>"
footer_file.close
navfile.close
ad_file.close
set main_obj = nothing
set foot_obj = nothing
set objfile = nothing
response.write(mainout)
response.write("</td></tr></table></body>")
response.write("</html>")
%>

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 3 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