RadTreeView for ASP.NET

Enumerating the child nodes of the selected TreeNode Send comments on this topic.
Example scenarios (How to) > Server-side > Enumerating the child nodes of the selected TreeNode

Glossary Item Box

To enumerate all the child nodes of the selected TreeNode you can use the approach shown in the example below. All the child nodes of the selected TreeNode will be selected upon a button click.

Example

ASPX Copy Code
<rad:RadTreeView
   
ID="RadTreeView1"
   
runat="server"
   
MultipleSelect="True">
</
rad:RadTreeView>
<
asp:Button
   
ID="Button1"
   
runat="server"
   
OnClick="Button1_Click" Text="Button" />
        
C# Copy Code
protected void Button1_Click(object sender, EventArgs e)
   {
       
if(RadTreeView1.SelectedNode!=null)
         CountNodes(RadTreeView1.SelectedNode.Nodes);
   }

protected void CountNodes(RadTreeNodeCollection nodes)
{
   
for(int i=0; i<nodes.Count; i++)
   {
       nodes[i].Selected = true;
       
if (nodes[i].Nodes.Count > 0)
       {
           CountNodes(nodes[i].Nodes);
       }
   }
}
        
VB.NET Copy Code
Protected Sub Button1_Click(sender As Object, e As EventArgs)
   If Not (RadTreeView1.SelectedNode Is Nothing) Then
      CountNodes(RadTreeView1.SelectedNode.Nodes)
   End If
End Sub 'Button1_Click

Protected Sub CountNodes(nodes As RadTreeNodeCollection)
   Dim i As Integer
   For i = 0 To nodes.Count - 1
      nodes(i).Selected = True
      If nodes(i).Nodes.Count > 0 Then
         CountNodes(nodes(i).Nodes)
      End If
   Next i
End Sub 'CountNodes