This example illustrates TreeView how to populate a node on the client using ASP.NET 2.0 and VB.NET.
This example illustrates TreeView how to populate a node on the client using ASP.NET 2.0 and C#.NET. First, you will need to import the using System.IO namespace.
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
We use the DirectoryInfo class for typical operations such as copying, moving, renaming, creating, and deleting directories. We then call the GetDirectories to returns the subdirectories of the current directory. Then we use the Treeview1_TreeNodePopulate event to do the work. Sometimes, it is not practical to statically predefine the tree structure due to data size or custom content that depends on user input. Because of this, the TreeView control supports dynamic node population. When a node's PopulateOnDemand property is set to true, that node is populated at run time through a postback event when the node is expanded. To populate a node dynamically, an event-handling method that populates the node must be defined for the TreeNodePopulate event. Supported browsers can also take advantage of client-side node population. When enabled, this allows the TreeView control to dynamically populate a node on the client when that node is expanded, which prevents the need to post back to the server.
Partial Class TreeViewnorefreshVB
Inherits System.Web.UI.Page
Protected Sub Treeview1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles Treeview1.TreeNodePopulate
If IsCallback Then
If (e.Node.ChildNodes.Count = 0) Then
LoadChildNode(e.Node)
End If
End If
End Sub
Protected Sub LoadChildNode(ByVal node As TreeNode)
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?amp;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 Sub
End Class
|
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
The front end Default.aspx page looks something like this:
<asp:treeview ID="Treeview1" runat="server" ImageSet="XPFileExplorer" AutoGenerateDataBindings="false" ExpandDepth=0
OnTreeNodePopulate="Treeview1_TreeNodePopulate">
<SelectedNodeStyle BackColor="#B5B5B5"></SelectedNodeStyle>
<Nodes>
<asp:TreeNode Value="D:" Text="D:" PopulateOnDemand="true" SelectAction="Select" NavigateUrl="#" >
</asp:TreeNode>
</Nodes>
<NodeStyle VerticalPadding="2" Font-Names="Tahoma" Font-Size="8pt" HorizontalPadding="2" ForeColor="Black"></NodeStyle>
<HoverNodeStyle Font-Underline="True" ForeColor="#6666AA"></HoverNodeStyle>
</asp:treeview>
|
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
The flow for the code behind page is as follows.
Imports System.IO
Partial Class TreeViewnorefreshVB
Inherits System.Web.UI.Page
Protected Sub Treeview1_TreeNodePopulate(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles Treeview1.TreeNodePopulate
If IsCallback Then
If (e.Node.ChildNodes.Count = 0) Then
LoadChildNode(e.Node)
End If
End If
End Sub
Protected Sub LoadChildNode(ByVal node As TreeNode)
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 Sub
End Class
|
Looking for the C# 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!