Joshua
Joshua

Reputation: 233

Hiding all Leaf Nodes while Showing Child Nodes on an ASP.Net Tree View

In my website, I have a TreeView that has this structure:

Root
|-Leaf
|-Parent
|--Leaf 
|--Leaf
|--Child
|---Leaf
|---Leaf
|--Child
|---Leaf
|---Leaf

Is it possible to make the tree in a "collapsed" state where all the Leaf nodes and ONLY leaf nodes are hidden from view until its parent node is expanded? The ideal solution would be to do this without a postback, but any solution at this time would be helpful.

The "collapsed" view would look like this:

Root
|-Parent
|--Child
|--Child

Expanding the Root, Parent or Child nodes should show their Leaf nodes. Collapsing the node should re-hide its leaf nodes, but leave the child nodes visible.

Upvotes: 1

Views: 2307

Answers (1)

NakedBrunch
NakedBrunch

Reputation: 49413

Here's a server-side (untested) solution:

TreeNodeCollection nodes = TreeView1.Nodes;
foreach (node in nodes)
{
   if (node.ChildNodes.Count == 0) 
   {
      node.Parent().Collapse();
   }
}

A client-side solution is definitely a preferred option.

Upvotes: 0

Related Questions