Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 120,120 times

Contents

Downloads

Related Categories

Tree structures in ASP.NET and SQL Server - Displaying Breadcrumbs

Displaying Breadcrumbs

One common theme when dealing with trees (especially in a web context) is to be able to provide what is known as a "breadcrumb" - that is, the path from the root of the tree, to the one we're looking at. For instance, on Developer Fusion the breadcrumb when you look at the ASP.NET section is Web Directory / Programming Languages / ASP.NET.

In order to do this, we need to fetch all rows whose lineage column is a prefix of the lineage column for the node we want to navigate to in the tree. For instance, if we're looking at a node childC whose lineage column is /root/childA/childC/, then we want to return the nodes with lineage columns /root/childA/ and /root/ - that is, the two nodes root and childA.

We'll use the LIKE operator again here, but this could equally be done using a combination of the LEFT and LEN functions.

CREATE PROCEDURE dfTreeGetPath ( @id INT ) AS

SELECT * FROM dfTree WHERE
    (SELECT lineage FROM dfTree WHERE id = @id) LIKE lineage + '%'
ORDER BY lineage, name

Although we've represented the tree in the SQL Server database, when we execute the dfTreeGetPath stored procedure, we still get a totally linear set of data back. For the moment though, we're not going to bother trying to re-create the tree structure by populating the TreeNode's Children collection. For one thing, it's a little complicated (and explained later), and secondly - we've got no need to. By sorting on the lineage and name columns, the rows are actually going to come back in a very sensible order - the root node first, and the node we're looking at last. To create our breadcrumb on a web page, we'll actually just end up binding an ASP.NET repeater control to this list of nodes.

Here's the code for our SqlServerTreeProvider class.

public ArrayList GetPath(int uniqueID)
{
    return ProcessList("dfTreeGetPath",
        new SqlParameter("@id",uniqueID));
}

In order to add our breadcrumb to the page we defined earlier, we add a new repeater to the page.

<asp:Repeater Runat="server" ID="breadcrumb">
    <ItemTemplate>
        <a href='<%# "TreeDemo.aspx?id=" + DataBinder.Eval(Container.DataItem,"UniqueID") %>'>
            <%# DataBinder.Eval(Container.DataItem,"Name") %></a>
    </ItemTemplate>
    <SeparatorTemplate> / </SeparatorTemplate>
</asp:Repeater>

And then simply add the following code to our Page_Load event.

breadcrumb.DataSource = treeProvider.GetPath(treeID);
breadcrumb.DataBind();

You'll also need to have populated the database with some dummy information by this point! After having clicked "Create Nodes" in your browser, you should see something like this:

A diagram displaying the structure of the table, with these fields: id (int, primary key), parentId (int), name (varchar), depth (int), lineage (varchar).

Looking a bit more hopeful now, isn't it? Because of the way we've designed the database, we've got no limit on the depth of the tree or the number of children that can be displayed. We can also store more than one tree in the database - by simply adding a new root node.

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Re: [4633] Tree structures in ASP.NET and SQL Server

    Posted by toranaga on 11 Mar 2007

    Here is how id did it to use with asp:tree:

    add this methods to the TreeNode class

    public string GetXml
        {
            get
     ...

  • Re: [4633] Tree structures in ASP.NET and SQL Server

    Posted by Goose99 on 24 Apr 2006

    Thanks this article has been a great help, I have one question.


    How hard would it be to get the tree f...

  • Nestedet model

    Posted by jcelko on 04 Nov 2005

    Here is my standard "cut & paste" on the Nested sets model for hierarchies.

    There are many ways to represent a tree or hierarchy in SQL. This is called an adjacency list model and it looks like th...

  • Posted by James Crowley on 03 Nov 2005

    Okay, point taken ... but as far as I'm aware if you moved the code I talk about out of triggers just into stored procs (or whatever), then the SQL is pretty standard stuff?

    I know that you're a ve...

  • Posted by jcelko on 03 Nov 2005

    Yoiu can do your heirarchies & trees in pure declarative SQL without using any proprieary 4GL like PL/SQL, T-SQL, etc.