INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

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.

Jobs

Controls

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}

CODE

<%@ 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
            PopulateNodes()
        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())
            TreeView1.Nodes.Add(masterNode)
            for each childRow As DataRow in _
              masterRow.GetChildRows("Children")
                Dim childNode As New TreeNode(childRow("ProductName").ToString())
                childNode.NavigateUrl = ("Default2.aspx?src=" & childRow("ProductName").ToString())
                masterNode.ChildNodes.Add(childNode)
            next
        Next
    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
    
</script>

<html>
<head>
    <title>Master/Detail TreeView</title>
</head>
<body>
    <form id="form1" runat="server">
    
    <asp:TreeView
        ID="TreeView1"
        Runat="Server"
        ShowExpandCollapse=true
        ImageSet=Faq
         ExpandDepth=0
        />
       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..
 
    </form>
</body>
</html>

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

My Archive

Resources

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