Use the TreeView Control with ASP.NET 4.0 and VB
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.
- Right click the project in your solution explorer.
- Select add new item…
- Select a web form.
- Name it ‘Default.aspx’.
- Click add.
- Open Default.aspx up to design mode.
- Drag and drop a treeview onto the web form.
- Change the ID property to ‘Treeview1′.
- Set the AutoGenerateDataBindings property to ‘False’.
- Add the following nodes to the treeview:
Code BlockDefault.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:
- Select the treeview.
- Click the Events icon in the Properties window.
- Double click the TreeNodePopulate event.
- Add the following code to that event method:
Code BlockDefault.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&t=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
