I'm using the Microsoft Web Control. I'm dynamically loading the control with data from the database where there are parent nodes and children nodes that could be up to 5 levels. I have a Javascript function that executes when a parent node is checked
to automatically select all the children below it. Now I have to figure out a way to find out what nodes are selected so that I can process them properly. The only way I see this happening is if I create a hidden variable and loop through all the nodes in
the TreeView and add the nodes to the variable if they are checked. However, I'm having a problem looping though the nodes. The only information I found was the SelectNodeIndex property which just gives me the node that was selected.
This is my TreeView Format:
<div mce_keep="true">Folder10 </div>
<div mce_keep="true">Folder1</div>
<div mce_keep="true">Item1</div>
<div mce_keep="true">Folder2</div>
<div mce_keep="true">Item2</div>
<div mce_keep="true">Folder 11</div>
<div mce_keep="true">Folder 3</div>
<div mce_keep="true">Item4</div>
<div mce_keep="true">Item5</div>
<div mce_keep="true">Folder 12</div>
<div mce_keep="true">Folder4</div>
<div mce_keep="true">Folder5</div>
<div mce_keep="true">Folder6</div>
<div mce_keep="true">Item6</div>
<div mce_keep="true">Folder 13</div>
<div mce_keep="true">Item7</div>
I need to find out which Item was selected in the TreeView. So lets say that I checked the Folder10 and Folder12 (all the subfolders and items are selected automatically). Then I want the hidden variable to hold all the selected nodes in this format;
Variable = Folder10_1_item1 (So the each parent node selected would start of with text "Folder" then the ID of the folder, then an "_" which means there is a child. Then the ID of the subFolder and then the text "item" and then the ID)
So If I selected Folder10 and Folder12 in the TreeView, I would like the variable to hold this information
Here's a couple of subs I put together to loop through all treenodes. I had to loop through the tree for a node with a specific value to select and expand it. hope this helps.
Private Sub RecurseNodes(ByVal searchValue
As Integer)
For Each tn
As TreeNode In TreeView1.Nodes
If tn.Value = searchValue
Then
tn.Expand()
tn.Select()
Exit For
End If
If tn.ChildNodes.Count > 0
Then
For
Each cTn As TreeNode
In tn.ChildNodes
recurseChildren(cTn, searchValue)
Next
End If
Next
End Sub
Private
Sub recurseChildren(ByVal tn
As TreeNode, ByVal searchValue
As Integer)
If tn.Value = searchValue
Then
tn.Expand()
tn.Select()
Exit Sub
End If
If tn.ChildNodes.Count > 0
Then
For
Each tnC As TreeNode
In tn.ChildNodes
I found that there is a property in the treeview CheckedNodes which gives list of nodes that are checked. Is there a opposite property which gives unchecked nodes from the treeview?
you will call with SelectNodesRecursive(what node to select ex:"10", name of the treeview ex: treeview1);
public static void SelectNodesRecursive(string searchValue,TreeView Tv)
{
foreach (TreeNode tn in Tv.Nodes)
{
if (tn.Value == searchValue)
{
tn.Expand();
tn.Select();
break;
}
if (tn.ChildNodes.Count > 0)
{
foreach (TreeNode cTn in tn.ChildNodes)
{
int a = SelectChildrenRecursive(cTn, searchValue);
if (a == 1)
{
tn.Expand();
}
}
}
}
}
andrea_johns...
Member
2 Points
5 Posts
Looping through TreeView Nodes?
Sep 11, 2007 04:35 PM|LINK
Hello,
I'm using the Microsoft Web Control. I'm dynamically loading the control with data from the database where there are parent nodes and children nodes that could be up to 5 levels. I have a Javascript function that executes when a parent node is checked to automatically select all the children below it. Now I have to figure out a way to find out what nodes are selected so that I can process them properly. The only way I see this happening is if I create a hidden variable and loop through all the nodes in the TreeView and add the nodes to the variable if they are checked. However, I'm having a problem looping though the nodes. The only information I found was the SelectNodeIndex property which just gives me the node that was selected.
This is my TreeView Format:
I need to find out which Item was selected in the TreeView. So lets say that I checked the Folder10 and Folder12 (all the subfolders and items are selected automatically). Then I want the hidden variable to hold all the selected nodes in this format;
Variable = Folder10_1_item1 (So the each parent node selected would start of with text "Folder" then the ID of the folder, then an "_" which means there is a child. Then the ID of the subFolder and then the text "item" and then the ID)
So If I selected Folder10 and Folder12 in the TreeView, I would like the variable to hold this information
Variable = Folder10_1_item1;Folder10_2_item2;Folder12_4_5_6_Item6
Can someone please help? How can I loop through each node of the treeview to achieve this.
Thank you
bsharp3
Member
4 Points
2 Posts
Re: Looping through TreeView Nodes?
Nov 09, 2007 11:42 PM|LINK
Here's a couple of subs I put together to loop through all treenodes. I had to loop through the tree for a node with a specific value to select and expand it. hope this helps.
Private Sub RecurseNodes(ByVal searchValue As Integer) For Each tn As TreeNode In TreeView1.Nodes If tn.Value = searchValue Thentn.Expand()
tn.Select()
Exit For End If If tn.ChildNodes.Count > 0 Then For Each cTn As TreeNode In tn.ChildNodesrecurseChildren(cTn, searchValue)
Next End If Next End Sub
Private Sub recurseChildren(ByVal tn As TreeNode, ByVal searchValue As Integer) If tn.Value = searchValue Thentn.Expand()
tn.Select()
Exit Sub End If If tn.ChildNodes.Count > 0 Then For Each tnC As TreeNode In tn.ChildNodesrecurseChildren(tnC, searchValue)
Next End If End SubRupen Shah
Member
9 Points
25 Posts
Re: Looping through TreeView Nodes?
Nov 12, 2007 08:56 PM|LINK
I found that there is a property in the treeview CheckedNodes which gives list of nodes that are checked. Is there a opposite property which gives unchecked nodes from the treeview?
asmvax
Member
4 Points
2 Posts
Re: Looping through TreeView Nodes?
Nov 25, 2007 11:28 AM|LINK
i've made some changes in your code:
you will call with SelectNodesRecursive(what node to select ex:"10", name of the treeview ex: treeview1);
public static void SelectNodesRecursive(string searchValue,TreeView Tv)
{
foreach (TreeNode tn in Tv.Nodes)
{
if (tn.Value == searchValue)
{
tn.Expand();
tn.Select();
break;
}
if (tn.ChildNodes.Count > 0)
{
foreach (TreeNode cTn in tn.ChildNodes)
{
int a = SelectChildrenRecursive(cTn, searchValue);
if (a == 1)
{
tn.Expand();
}
}
}
}
}
private static int SelectChildrenRecursive(TreeNode tn, string searchValue)
{
if (tn.Value == searchValue)
{
//if(tn.Parent.Parent!=null)
//{
// tn.Parent.Parent.Expand();
//}
//if (tn.Parent != null)
//{
// tn.Parent.Expand();
//}
tn.Expand();
tn.Select();
return 1;
}
else
{
// tn.Parent.Collapse();
tn.Collapse();
}
if (tn.ChildNodes.Count > 0)
{
foreach (TreeNode tnC in tn.ChildNodes)
{
int a = SelectChildrenRecursive(tnC, searchValue);
if (a == 1)
{
tn.Expand();
return 1;
}
}
}
return 0;
}
darkcat02
Member
178 Points
151 Posts
Re: Looping through TreeView Nodes?
Mar 23, 2009 07:46 AM|LINK
hi there...
can you give me an example how did you execute those functions? i need it badly.. thanks a lot...
Mhaey
Please remember to click “Mark as Answer” on the post that helps you.. =)
Sathieshkuma...
Member
2 Points
1 Post
Re: Looping through TreeView Nodes?
Jan 29, 2010 11:18 AM|LINK
This code helped me.
thanks.
danieladacru...
Member
8 Points
4 Posts
Re: Looping through TreeView Nodes?
Mar 06, 2010 11:08 AM|LINK
Hi.
I'm using also the TreeView control to display an XML file (with a XMLDataSource).
It works great. It is very fast even when the document is big.
However, I would like to display how long the TreeView takes to be displayed. Any idea?
I could not do this after make the DataBind because this would given like 2 or 3 seconds. But sometimes, it takes minutes...
thanks
daniela
TreeView