Select the first leaf node of a tree

3 posts, 0 answers
  1. Nataraj Vedula
    Nataraj Vedula avatar
    21 posts
    Member since:
    Aug 2008

    Posted 05 Nov 2008 Link to this post

    Hi,

    I have a Rad tree view bound to a dataset. When page loads, i want to display the tree view with the first leaf node in the tree structure. for Eg:
    Consider tree has the following structure:
    A:-
        A1:-
            A11:-
                A111
                A112
                A113
            A12:-
                A121
                A122
            A13
        A2:-
            A21:-
                A211
                A212:-
                    A2121
            A22
        A3
    B:-
        B1:-
        so on...

    when page loads i want to show the user node A11 selected and also parent nodes for A11 expanded.(if A11 has no child nodes then A11 has to be selected as it will be first leaf node) How can i do this server side?

    I tried to do it by loops but i am not successful to traverse through the nodes at diefferent levels.

    Please help me.

    Thanks in advance,
    Nataraj
  2. Simon
    Admin
    Simon avatar
    2281 posts

    Posted 05 Nov 2008 Link to this post

    Hello Nataraj Vedula,

    You can use the following routines to achieve your goal:

    [C#]
    private void LocateFirstLeafNode() 
        RadTreeNode node = GetFirstLeafNode(this.RadTreeView1.Nodes); 
     
        if (node != null
        { 
            node.Selected = true
     
            node.ExpandParentNodes(); 
        } 
     
    private RadTreeNode GetFirstLeafNode(RadTreeNodeCollection nodes) 
        RadTreeNode firstLeafNode = null
     
        if (nodes.Count > 0) 
        { 
            if (nodes[0].Nodes.Count == 0) 
            { 
                firstLeafNode = nodes[0]; 
            } 
            else 
            { 
                firstLeafNode = GetFirstLeafNode(nodes[0].Nodes); 
            } 
        } 
     
        return firstLeafNode; 

    [VB.NET]
    Private Sub LocateFirstLeafNode() 
        Dim node As RadTreeNode = GetFirstLeafNode(Me.RadTreeView1.Nodes) 
     
        If node IsNot Nothing Then 
            node.Selected = True 
     
            node.ExpandParentNodes() 
        End If 
    End Sub 
     
    Private Function GetFirstLeafNode(ByVal nodes As RadTreeNodeCollection) As RadTreeNode 
        Dim firstLeafNode As RadTreeNode = Nothing 
     
        If nodes.Count > 0 Then 
            If nodes(0).Nodes.Count = 0 Then 
                firstLeafNode = nodes(0) 
            Else 
                firstLeafNode = GetFirstLeafNode(nodes(0).Nodes) 
            End If 
        End If 
     
        Return firstLeafNode 
    End Function 

    I hope this is useful.

    Best wishes,
    Simon
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.
  3. Nataraj Vedula
    Nataraj Vedula avatar
    21 posts
    Member since:
    Aug 2008

    Posted 05 Nov 2008 Link to this post

    It works great. Thanks Simon.

    Regards,
    Nataraj
Back to Top