Apr 23 2006

Traverse a TreeView in ASP.NET 2.0

Category: ASP.NET 2.0 - GeneralBil@l @ 13:54

I'm working those days on an application, which relies a lot on TreeView Control in ASP.NET 2.0. However, it is not just working with it out of the box, I am forced to go down and program with the control.

However, I was trying to traverse a Tree and thought of sharing with you this piece of code:

    private void TraverseTreeView(TreeNodeCollection p_NodeCollection, int p_Level)
    {
        foreach (TreeNode _TreeNode in p_NodeCollection)
        {
            for (int i = 0; i < p_Level; i++)
            {
                Response.Write("&nbsp;&nbsp;&nbsp;");
            }

            if (_TreeNode.ChildNodes.Count > 0)
            {
                Response.Write("<b>" + _TreeNode.Text + "</b>" + "<br>");
                TraverseTreeView(_TreeNode.ChildNodes, p_Level + 1);
            }
            else
            {
                Response.Write(_TreeNode.Text + "<br>");
            }
        }
    }

You would call this method as:

TraverseTreeView(this.TreeView1.Nodes, 0);

 

Hope this helps you working with your TreeView.

Regards

Tags:

Comments are closed