Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Populate an ActiveX TreeView Control Using ASP


View products that this article applies to.

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).

↑ Back to the top


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

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

↑ Back to the top


Keywords: KB183329, kbhowto

↑ Back to the top

Article Info
Article ID : 183329
Revision : 6
Created on : 3/14/2005
Published on : 3/14/2005
Exists online : False
Views : 592