Home All Groups Group Topic Archive Search About

Re: How to get underlying data of TreeView control? (ASP.NET 2.0)

Author
29 Mar 2006 6:18 AM
misiek
Anybody?

Author
29 Mar 2006 12:14 PM
CaffieneRush@gmail.com
For your first problem. I don't believe the treeview supports 2 way
databinding - its only one way from data source control or object to
the treeview but not back. Besides you either associate your treeview
to a data source control (like XMLDataSource) using the DataSourceID
property or a data object (like DataSet) using the DataSource property
but not both.
You'll need to work with the treeview directly on the postback but I
might be wrong.

For your second problem. There is no node id property within treenode,
unless of course you subclass treenode and provide an id.
What I do is use the name or value property of treenode to locate the
node within the tree. Of course that assumes that the text and value
are unique to each node.

Eg. find the node by text.

Public Function GetNodeByText(ByVal text As String) As MyTreeNode
            Return GetNodeByText(text, Nodes)
End Function

Public Function GetNodeByText(ByVal text As String, ByVal childNodes As
TreeNodeCollection) As MyTreeNode

            Dim node As MyTreeNode

            For Each node In childNodes
                'Search immediate children first
                If node.Text = text Then
                    'Found node with matching text.
                    Exit For
                End If

                'If children themselves have children then search those
as well
                If node.ChildNodes.Count > 0 Then
                    Return GetNodeByText(text, node.ChildNodes)
                End If
            Next

            Return node

End Function

Hope that helps.
Author
29 Mar 2006 2:29 PM
misiek
HI Caffiene.

Thanks for the answer.

Unfortunately Text value of some of my nodes could be the same.
However, they could be distinguished by some attribute value:
every node has QUSTION_ID attribute, which is unique. On the other side
I do not display value of that attribute.

Is there some way to compare nodes based on this attribute?

Best regards,
placek
Author
29 Mar 2006 10:37 PM
CaffieneRush@gmail.com
I can't see a problem with finding nodes by the QUESTION_ID attribute.

Public Function GetNodeByQID(ByVal QID As String) As MyTreeNode
            Return GetNodeByQID(QID, Nodes)
End Function

Public Function GetNodeByQID(ByVal QID As String, ByVal childNodes As
TreeNodeCollection) As MyTreeNode

            Dim node As MyTreeNode

            For Each node In childNodes
                'Search immediate children first
                If node.Attributes("QUESTION_ID") = QID Then
                    'Found node with matching text.
                    Exit For
                End If

                'If children themselves have children then search those
as well
                If node.ChildNodes.Count > 0 Then
                    Return GetNodeByQID(QID, node.ChildNodes)
                End If
            Next

            Return node

End Function

Regards.
Author
30 Mar 2006 6:32 AM
misiek
I did not know that it is so simple!

Thanks a lot, Caffiene.

Best regards
Author
30 Mar 2006 10:04 AM
CaffieneRush@gmail.com
My pleasure.