Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Microsoft: ASP.NET FAQ


How can you Databind an ASP 2.0 Treeview Control to Database data for use as a Menu.. by NoCoolHandle
Posted: 17 Jan 06

The treeview control is one of Asp 2.0 best new additions. It allows you to construct truly nice menu's with expand/contract capacity and allows for custom images and supports a number of nice built-in themes.

It's strength (and weakness) is that it uses Hierarchal Data. This means that it is very easy to just bind it to an XML file (via an XMLDatasource object) and display complex menus.

However it becomes less obvious as to how you can use it with a dynamic datasource like a query.

The issue revolves around how to create usable hierarchal data to bind it to.

The trick is to use a dataasets DataRelation object. and then to bind each level of the treeview to the correct datatable.

The more levels of menu you need the more datatables you will need.

The following example shows how to use 2 querys to display all the products in the northwind database by category..

(this code was adapted from information in the Whidbey help files. For more help search on "Introducing the ASP.NET 2.0 TreeView and Menu Controls" by Stephen Walther)

(beware of line wrapping}


<%@ Page Language="VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>

<script runat="server">

    Const connectionString As String = _
      "Server=localhost;Integrated Security=True;Database=Northwind"
    Sub Page_Load()
        if NOT IsPostBack Then
        End If
    End Sub

    Sub PopulateNodes()
        Dim dst As DataSet = GetTreeViewData()
        for each masterRow As DataRow in dst.Tables("Categories").Rows
            Dim masterNode As New TreeNode(masterRow("CategoryName").ToString())
            masterNode.NavigateUrl = ("Default2.aspx?src=" & masterRow("CategoryName").ToString())
            for each childRow As DataRow in _
                Dim childNode As New TreeNode(childRow("ProductName").ToString())
                childNode.NavigateUrl = ("Default2.aspx?src=" & childRow("ProductName").ToString())
    End Sub   
    Function GetTreeViewData() As DataSet
        Dim con As SqlConnection = new SqlConnection(connectionString)
        Dim dadCats As SqlDataAdapter = new _
          SqlDataAdapter("SELECT * FROM Categories", con)
        Dim dadProducts As SqlDataAdapter = new _
          SqlDataAdapter("SELECT * FROM Products", con)
        Dim dst As DataSet = new DataSet()
        dadCats.Fill(dst, "Categories")
        dadProducts.Fill(dst, "Products")
        dst.Relations.Add("Children", _
            dst.Tables("Categories").Columns("CategoryID"), dst.Tables("Products").Columns("CategoryID"))
        return dst
    End Function

    <title>Master/Detail TreeView</title>
    <form id="form1" runat="server">
       To modify the look of the control try editing the "ImageSet" property of the treeview control...<br><br>
You can also use the imageUrl attribute of the node to provide custom images for the treeview..

Back to Microsoft: ASP.NET FAQ Index
Back to Microsoft: ASP.NET Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close