Pages with Dynamic Content
So far, this sample DLL can send multiple static HTML pages. But the whole point of a Web application is to process pages with dynamic content. If the HTML for a page is contained in a separate, static file, how can it have dynamic content?
My solution is to use placeholder "tokens" for the variable content when designing the HTML, and then have the code replace the tokens with the actual content just before it is sent to the browser. These tokens are just strings that can be easily found by your VB code, and that aren't likely to otherwise occur in the HTML code. For example, Figures 4 and 5 (below) show the HTML design for the two pages that are used in this sample application. On the Search Publishers form, which is shown in Figure 4, name the Filter text element txtFilter.
Replacing the tokens in code is trivial with VB6. VB6 added a new string function, Replace, that searches a string variable and replaces all occurrences of one string value with another. (If you use an older version of VB, you'll need to write, or find, a similar function.) For example, the SearchSend procedure should be coded as follows:
Private Sub SearchSend() Dim lHTML As String lHTML = LoadHTML("PublisherSearch.htm") lHTML = Replace(lHTML, "{Filter}", "") lHTML = Replace(lHTML, "{PublishersTable}", "") Call gAspResp.Write(lHTML) End Sub
To complete the application, change your project to reference the ADO Object Library (such as Microsoft ActiveX Data Objects 2.5 Library), and implement the SearchProc and DetailProc procedures, as shown in this listing:
Private Sub SearchProc() Dim lHTML As String Dim lTable As String Dim lDB As ADODB.Connection Dim lRS As ADODB.Recordset ' Query the database for the matching Publishers and store ' the results into the lTable string array. Set lDB = New ADODB.Connection Call lDB.Open("Driver={Microsoft Access Driver (*.mdb)}; DBQ=" _ & App.Path & "\Biblio.mdb") Set lRS = lDB.Execute("SELECT * FROM Publishers" _ & " WHERE Name LIKE '%" & gAspReq.Item("txtFilter") & "%'" _ & " ORDER BY Name") lTable = "<table border=1><TR><TD>Name</TD><TD>Telephone</TD></TR>" Do Until lRS.EOF = True ' Hyperlink to show the detail for this publisher. lTable = lTable & "<TR><TD><a href='PubWebUI.asp?action=DetailProc&PubID=" _ & lRS!PubID & "'>" & lRS!Name & "</TD>" lTable = lTable & "<TD>" & lRS!Telephone & "</TD></TR>" Call lRS.MoveNext Loop lTable = lTable & "</table>" ' Load the HTML file and replace the tokens. lHTML = LoadHTML("Search.htm") lHTML = Replace(lHTML, "{Filter}", gAspReq.Item("txtFilter")) lHTML = Replace(lHTML, "{PublishersTable}", lTable) ' Send the HTML. Call gAspResp.Write(lHTML) End Sub Private Sub DetailProc() Dim lHTML As String Dim lDB As ADODB.Connection Dim lRS As ADODB.Recordset ' Query the database for the indicated Publisher. Set lDB = New ADODB.Connection Call lDB.Open("Driver={Microsoft Access Driver (*.mdb)}; DBQ=" _ & App.Path & "\Biblio.mdb") Set lRS = lDB.Execute("SELECT * FROM Publishers" _ & " WHERE PubID = " & gAspReq.QueryString.Item("PubID")) ' Load the HTML file. lHTML = LoadHTML("Detail.htm") ' Replace the tokens in the HTML with the Publisher data. lHTML = Replace(lHTML, "{Name}", "" & lRS!Name) lHTML = Replace(lHTML, "{Company}", "" & lRS![Company Name]) lHTML = Replace(lHTML, "{Address}", "" & lRS!Address) lHTML = Replace(lHTML, "{City}", "" & lRS!City) lHTML = Replace(lHTML, "{State}", "" & lRS!State) lHTML = Replace(lHTML, "{Zip}", "" & lRS!Zip) lHTML = Replace(lHTML, "{Telephone}", "" & lRS!Telephone) lHTML = Replace(lHTML, "{Fax}", "" & lRS!Fax) ' Send the HTML. Call gAspResp.Write(lHTML) End Sub
After you have completely tested your code, compile it into a DLL file. Then, to install your new Web app, simply register the DLL on the Web server using Regsvr32.exe, and copy the tiny PubWebUI.asp file to the Web server somewhere so that it can be requested by a browser.