Creating the Form
To demonstrate how to populate a tree view control with links, we will need to create a simple web form. To begin, create a new ASP.NET Empty Web Site and:

I just signed up at Server Intellect and couldn’t be more pleased with my fully scalable & redundant cloud hosting! Check it out and see for yourself.

  1. Right click the project in your solution explorer.
  2. Select add new item…
  3. Select a web form.
  4. Name it ‘Default.aspx’.
  5. Click add.
  6. Open Default.aspx up to design mode.
  7. Drag and drop a treeview onto the web form.
  8. Change the ID property to ‘Treeview1′.
  9. Set the AutoGenerateDataBindings property to ‘False’.
  10. Add the following nodes to the treeview:
    Code Block
    Default.aspx

    The top node of our tree view.

    <Nodes>
        <asp:TreeNode Value="C:" Text="C:" PopulateOnDemand="true" SelectAction="Select" NavigateUrl="#" >
        </asp:TreeNode>
    </Nodes>

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

Once we have our tree view setup we need to add some code that will populate it with some links. To do this, open Default.aspx up to design mode and:

  1. Select the treeview.
  2. Click the Events icon in the Properties window.
  3. Double click the TreeNodePopulate event.
  4. Add the following code to that event method:
    Code Block
    Default.aspx.vb

    The Treeview1_TreeNodePopulate event method.

    If IsCallback Then
        If (e.Node.ChildNodes.Count = 0) Then
            Dim node As TreeNode = e.Node

            Dim directory As New DirectoryInfo(node.Value)
            Dim s As DirectoryInfo
            Dim fi As FileInfo
            For Each s In directory.GetDirectories()
                Dim subNode As New TreeNode(s.Name)
                subNode.Value = s.FullName
                Try
                    If s.GetDirectories().Length > 0 Or s.GetFiles().Length > 0 Then
                        subNode.SelectAction = TreeNodeSelectAction.SelectExpand
                        subNode.PopulateOnDemand = True
                        subNode.NavigateUrl = "#"
                    End If
                Catch
                    subNode.ImageUrl = "WebResource.axd?a=s&r=TreeView_XP_Explorer_ParentNode.gif&ampt=632242003305625000"
                    node.ChildNodes.Add(subNode)
                End Try
            Next
            For Each fi In directory.GetFiles()
                Dim subNode As New TreeNode(fi.Name)
                node.ChildNodes.Add(subNode)
            Next
        End If
    End If

Yes, it is possible to find a good web host. Sometimes it takes a while to find one you are comfortable with. After trying several, we went with Server Intellect and have been very happy thus far. They are by far the most professional, customer service friendly and technically knowledgeable host we’ve found so far.

Testing
To test this out load up the web site. Ensure that the tree view has some links in it and expands when you click it.
TreeView asp4 vb