HOWTO: Populate an ActiveX TreeView Control Using ASP

ID: Q183329


The information in this article applies to:
  • Microsoft Data Access Components versions 2.0, 2.1 SP2
  • Active Server Pages
  • Microsoft Internet Information Server versions 4.0, 5.0


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

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

Additional query words: ASP, TreeView Control, ActiveX

Keywords : kbActiveX kbASP kbGrpASP kbGrpMDAC kbDSupport kbADO210sp2 kbiis400 kbiis500
Version : WINDOWS:2.0,2.1 SP2; winnt:4.0,5.0
Platform : WINDOWS winnt
Issue type : kbhowto


Last Reviewed: December 8, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.