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

P

placek

Hi all.

I have a page with a TreeView control and a XmlDataSource. I set
XmlDataSource.Data property with some DataSet.GetXML() method and then
I programatically set TreeView.DataSourceId to my XmlDataSource. It
works fine, all data is properly displayed. One more thing: this
TreeView control is placed in WizardControl.

My problem starts when user clicks Next button in WizardControl. At
this moment I would like to retrieve underlying data of my TreeView
control. Unfortunately, TreeView.DataSource seems to be null.

I tried to store XmlDataSource.Data in Session at the moment, when data
is bound, but this approach is not good for me. The reason is that it
stores initial data values. In fact I need to retrieve current data
values. Why? Because user could for example (un)check some checkboxes
from TreeView and I would like to know about that. I know, that there
is a TreeView.CheckedNodes property, but I need to retrieve ALL
structure, not only changed nodes.

Is there some way to retrieve TreeView data, which is currently
displayed in browser?

My second problem is how to find some node in XML file, when I iterate
through TreeView.CheckedNodes collection. I know that there is a
GetElementById() method of XmlDocument, but I have no idea how to get
node id from some node.

Does anybody can help me?

Thanks in advance,
misiek.
 
C

CaffieneRush

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.
 
M

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
 
C

CaffieneRush

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top