Find TreeNode.Text in Treeview

byoung

Newcomer
Joined
Jan 23, 2003
Messages
9
Anyone know of a quick to find a value in a Treeview by index or going directly to the text value?

I know you can use recursive methods and loop through TreeNode collections, etc.

Some code examples would be very much appreciated...

Thanks!

Barry
 
You can't. The only way you could do this is by keeping a hashtable object around to associate text values with treenode objects as you add them. If this would be useful to you, I suggest looking up the Hashtable class in the help.
 
Well, actually you can do this. I hope this might help...

Code:
'assuming treeView1 control has been added to form

'Sample XML
<TREENODES><treenode text="Text to be displayed on page" NodeData="hidden data value associated to displayed text"></treenode></TREENODES>


'Load XML into treeView control static or dynamic

'Static
treeView1.TreeNodeSrc = "TreeViewControl.xml"

'Dynamic
Dim sMyXML as String
Dim myXMLDocument as XmlDocument

'Build your XML string however you need to
sMyXML = "<TREENODES><treenode text=""Text to be displayed on page"" NodeData=""hidden data value associated to displayed text""></treenode></TREENODES>"

myXMLDocument.LoadXML(sMyXML)
treeView1.TreeNodeSrc = myDomDocument.InnerXML
treeView1.DataBind()


'Code for treeView1_SelectedIndexChange
'AutoPostBack property must be set to true for this event to fire on each node select, otherwise the events are queued

dim tnSelectedNode as TreeNode

tnSelectedNode = treeView1.GetNodeFromIndex(treeView1.SelectedNodeIndex)

Response.write("Selected node display text:  " & tnSelectedNode.Text & "<br>")
Response.write("Selected node hidden value:  " & tnSelectedNode.NodeData & "<br>")

That should do it
 
Back
Top