The TreeNodeCollection
Skip Navigation Links.

The TreeNodeCollection

Skip Navigation LinksHome :: ASP.NET :: Controls :: TreeNodeCollection

The TreeNodeCollection

Here's some code to help you write out the TreeNodeCollection from a treeview control
Skip Navigation Links.

Table of Contents
Chapter One
Section 1.0
Topic 1.0.1
Topic 1.0.2
Topic 1.0.3
Section 1.1
Topic 1.1.1
Topic 1.1.2
Topic 1.1.3
Topic 1.1.4
Chapter Two
Section 2.0
Topic 2.0.1
Subtopic 1
Subtopic 2
Topic 2.0.2
Appendix A
Appendix B
Appendix C
Here's the code...

 Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

    ' If the TreeView control contains any root nodes, perform a
    ' preorder traversal of the tree and display the text of each node.
    If LinksTreeView.Nodes.Count > 0 Then

      ' Iterate through the root nodes in the Nodes property.
      Dim i As Integer

      For i = 0 To LinksTreeView.Nodes.Count - 1

        ' Display the nodes.
        DisplayChildNodeText(LinksTreeView.Nodes(i))

      Next i

    Else

      Message.Text = "The TreeView control does not have any nodes."

    End If
								If Not IsPostBack Then

												' Use the Add and Remove methods to programmatically 
												' remove the Appendix C node and replace it with a new 
												' node. 
												LinksTreeView.Nodes.Remove(LinksTreeView.Nodes(3))
												LinksTreeView.Nodes.Add(New TreeNode("New Appendix C"))

												' Use the AddAt and RemoveAt methods to programmatically 
												' remove the Chapter One node and replace it with a new node.
												LinksTreeView.Nodes(0).ChildNodes.RemoveAt(0)
												LinksTreeView.Nodes(0).ChildNodes.AddAt(0, New TreeNode("New Chapter One"))

												' Use the Clear method to remove all the child nodes of the 
												' Chapter Two node.
												LinksTreeView.Nodes(0).ChildNodes(1).ChildNodes.Clear()

								End If


  End Sub

  Sub DisplayChildNodeText(ByVal node As TreeNode)

    ' Display the node's text value.
    Message.Text &= node.Text & "
" ' Iterate through the child nodes of the parent node passed into ' this method and display their values. Dim i As Integer For i = 0 To node.ChildNodes.Count - 1 ' Recursively call the DisplayChildNodeText method to ' traverse the tree and display all the child nodes. DisplayChildNodeText(node.ChildNodes(i)) Next i End Sub
Next >> How To
top of page
all content ©1996/2012 BennySutton.com
all images © their respective owners This site uses Thumbshots previews