Which node was selected?

Consider the following XML fragment...

<channel>
...
    <item>
      <title>John</title>
       ...
    </item>
    <item>
      <title>Jane</title>
       ...
    </item>
</channel>

When the file is loaded into the TreeView control and a <title> node is selected I have no idea how to determine how to identify which parent the <title> actually descends from.

The TreeNode.Parent property returns the value of the node's parent. The value would be 'item' but I need to identify which parent and which item as there are multiple instances of the <item> element in the file which have the same value.

The broader objective is to determine which node has been selected to enable editing the data in the selected node.

Please paste your code if handy but advising me of the right methodology or referral to documents will be just as appreciated. Thanks for your comments...

<%= Clinton Gallagher



Answer this question

Which node was selected?

  • Ron Walker

    Post this on http://forums.asp.net forums.

  • XyMeXian Archer

    Some times the following code will help you. To call this function, just do like this.
    GetParent(MyTreeview, MyNode).

    Private Function GetParent(ByVal Treeview1 As TreeView, ByVal node As TreeNode, _

    Optional ByVal startNode As TreeNode = Nothing, _

    Optional ByRef gotIt As Boolean = False) As TreeNode

    Dim MyNodeCollection As TreeNodeCollection

    Dim TempNode As TreeNode

    Dim TempGotIt As Boolean

    ' start from where

    If startNode Is Nothing Then

    MyNodeCollection = TreeView1.Nodes

    Else

    MyNodeCollection = startNode.Nodes

    End If

    ' loop through the nodes collection

    For Each nd As TreeNode In MyNodeCollection

    ' got it

    If nd.GetNodeIndex = node.GetNodeIndex Then

    If startNode Is Nothing Then

    gotIt = True

    Return Nothing

    Else

    gotIt = True

    Return startNode

    End If

    End If

    ' if not get it, then search in child nodes

    TempNode = GetParent(Treeview1, node, nd, TempGotIt)

    If TempGotIt = True Then

    Return TempNode

    End If

    Next

    End Function


  • Dino Ablakovic

     Kishore Ramakrishnan wrote:
    Some times the following code will help you. To call this function, just do like this.
    GetParent(MyTreeview, MyNode).

    <snip />



    I didn't see a GetParent method %-( Thanks Kishore.


  • Imti

    TreeNode.Tag can store anything and what wrong with TreeNode.Parent :-)
  • Which node was selected?