Microsoft KB Archive/183329

= How To Populate an ActiveX TreeView Control Using ASP =

Article ID: 183329

Article Last Modified on 3/14/2005

-

APPLIES TO


 * Microsoft Data Access Components 2.0
 * Microsoft Data Access Components 2.1 Service Pack 2
 * Microsoft Data Access Components 2.5
 * Microsoft Data Access Components 2.6
 * Microsoft Data Access Components 2.7
 * Microsoft Active Server Pages 4.0
 * Microsoft Internet Information Server 4.0
 * Microsoft Internet Information Services 5.0

-



This article was previously published under Q183329



SUMMARY
This article describes how to use Active Server Pages (ASP) code to populate an ActiveX TreeView control. The technique shown here leverages ASP's ability to dynamically build a VBScript function to populate a client-side ActiveX control, based on results retrieved through ActiveX Database Objects (ADO).



MORE INFORMATION
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" %>

   Sample Tree Control         <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>

Additional query words: ASP TreeView Control ActiveX

Keywords: kbhowto KB183329

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.