Closing Treeview nodes automatically

D

David Jackson

Hello,

I have a TreeView control with "top-level" nodes and "second-level" nodes
(probably not the correct terms) as follows:

objNode = new TreeNode();
objNode.Text = "2006";
objNode.SelectAction = TreeNodeSelectAction.Expand;
tvSide.Nodes.Add(objNode);

objChildNode = new TreeNode();
objChildNode.Text = " ·January";
objChildNode.NavigateUrl = "../2006/jan.aspx";
objNode.ChildNodes.Add(objChildNode);

objChildNode = new TreeNode();
objChildNode.Text = " ·February";
objChildNode.NavigateUrl = "../2006/feb.aspx";
objNode.ChildNodes.Add(objChildNode);

// other child nodes for 2006

objNode = new TreeNode();
objNode.Text = "2007";
objNode.SelectAction = TreeNodeSelectAction.Expand;
tvSide.Nodes.Add(objNode);

objChildNode = new TreeNode();
objChildNode.Text = " ·January";
objChildNode.NavigateUrl = "../2007/jan.aspx";
objNode.ChildNodes.Add(objChildNode);

objChildNode = new TreeNode();
objChildNode.Text = " ·February";
objChildNode.NavigateUrl = "../2007/feb.aspx";
objNode.ChildNodes.Add(objChildNode);

When I click on the 2006 node, it correctly expands to reveal the nodes
underneath it.

But, if I then click on the 2007 node, I would like it to "close up" the
2006 node as well as expanding the 2007 node. Is there a way to do this?

Thank you.

DJ
 
G

Guest

Hi, I have done this in Javascript. You can actually view source of the page
and write a generic code to handle it.

-Geo.
 
D

David Jackson

Hello Geo,
Hi, I have done this in Javascript. You can actually view source of the
page
and write a generic code to handle it.

Excellent suggestion! The following code will do this, if anyone else is
interested:

<script type="text/javascript">
var base_TreeView_ToggleNode = TreeView_ToggleNode;
TreeView_ToggleNode = function(data, index, node, lineType, children)
{
var objTreeView = document.getElementById('<%=tvSide.ClientID%>');
for (intChild = 0; intChild < objTreeView.children.length;
intChild++)
{
if (objTreeView.children[intChild].id.indexOf('Nodes') > -1)
{
objTreeView.children[intChild].style.display = "none";
}
}
base_TreeView_ToggleNode(data, index, node, lineType, children);
}
</script>

N.B. this could probably be made more robust by checking that the id of the
children element ends with 'Nodes' rather than just contains 'Nodes', and
the TreeView_ToggleNode function does a fair bit more than just setting the
display style, but the above works for me and is all I need at the moment.

DJ
 
M

Matias Banzas

Hi guys. I have a treeview that should close is nodes when one of them is expanded just as this example. But there is a "catch"-

My treeview structure is as follows

RootNode
--------Child1
----------Sub1Child1
----------Sub1Child2
--------Child2
----------Sub2Child1
----------Sub2Child2

If I expand Sub1Child1 then Sub1Child2 and Child2 should be Collapsed.

Can you give me a hand here? I've been trying to do this for some time now and got no results :S

Thanks

EggHeadCafe - .NET Developer Portal of Choice
http://www.eggheadcafe.com
 
G

Geo

Hi, Just wondering whether you got to do this. If not let me know and I can
help you. I did the same in one of my projects.

Thanks
Geo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top