The following example uses ASP code to populate an ActiveX TreeView
control. This sample uses the Pubs sample database provided with Microsoft
SQL Server. You need to create a data source name (DSN) called PUBS on your
Web server that points to the Pubs database to run this sample.
This code demonstrates how ASP uses ADO to execute a SELECT statement that
retrieves a list of authors and their associated book titles. The result is
a TreeView control that lists the authors as parent nodes of the control
while the corresponding book titles appear as child nodes.
Sample Code
<%@ LANGUAGE="VBSCRIPT" %>
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" content="text/html; charset=iso-8859-1">
<TITLE>Sample Tree Control</TITLE>
</HEAD>
<BODY>
<OBJECT ID="TreeView1" WIDTH=300 HEIGHT=400
CLASSID="CLSID:0713E8A2-850A-101B-AFC0-4210102A8DA7">
<PARAM NAME="_ExtentX" VALUE="7938">
<PARAM NAME="_ExtentY" VALUE="10583">
<PARAM NAME="_Version" VALUE="327682">
<PARAM NAME="LineStyle" VALUE="1">
<PARAM NAME="Style" VALUE="6">
<PARAM NAME="Appearance" VALUE="1">
</OBJECT>
<SCRIPT LANGUAGE="VBScript">
TreeView1.Font.Name = "Arial"
TreeView1.Font.Size = 10
<%
'************ Construct SQL Statement ******************************
sSQL = "SELECT authors.au_id, authors.au_fname + ' ' +
authors.au_lname as 'author', titles.title"
sSQL = sSQL & " FROM authors"
sSQL = sSQL & " JOIN titleauthor ON authors.au_id=titleauthor.au_id"
sSQL = sSQL & " JOIN titles ON titleauthor.title_id =
titles.title_id"
sSQL = sSQL & " ORDER BY authors.au_lname, authors.au_fname,
authors.au_id, titles.title"
'************ Create Connection and Recordset **********************
Set cnnPubs = Server.CreateObject("Adodb.connection")
cnnPubs.open "DSN=pubs", "sa" 'Point to a DSN called PUBS.
Set rstAuthorsTitles = Server.CreateObject("adodb.recordset")
Set rstAuthorsTitles = cnnPubs.execute(sSQL)
'************ Populate the Control ******************************
Dim sTmpAuthorId
sTmpAuthorId = ""
Do While Not rstAuthorsTitles.EOF 'Check for end of recordset.
If rstAuthorsTitles(0) <> sTmpAuthorId Then 'Check for empty or
'the same author_id.
Response.Write "TreeView1.Nodes.Add , , " & Chr(34) &_
rstAuthorsTitles(0) & Chr(34) & "," & Chr(34) &_
rstAuthorsTitles(1) & Chr(34) & chr(10) & chr(13)
'Add author to root node.
sTmpAuthorId = rstAuthorsTitles(0)
End If
'********* Add the titles as a child node to the author *****
Response.Write Chr(10) & "TreeView1.Nodes.Add " & Chr(34) & _
rstAuthorsTitles(0) & Chr(34) & ", 4,," & Chr(34) & _
rstAuthorsTitles(2) & Chr(34) & chr(10) & chr(13)
rstAuthorsTitles.MoveNext
Loop
%>
</SCRIPT>
</BODY>
</HTML>