This tutorial will show you how to use the TreeView control to allow clicking of files to open from your web browser. VB version.
The TreeView control is very versatile, in that we can assign a variety of sources to it and it will display in the same way - showing us a hierarchical structure of the data source.
In this tutorial, we will be looking at how we can use the TreeView to display a directory of our website and how to make the files clickable so that we can open them up from the browser.
The first thing we need to do is create our ASPX page. We will add a TreeView control:
| <form id="form1" runat="server"> <asp:TreeView ID="TreeView1" runat="server" OnTreeNodePopulate="TreeView1_TreeNodePopulate" </form> AutoGenerateDataBindings="False" ExpandDepth=0> </asp:TreeView><Nodes> <asp:TreeNode Value="C:" Text="media" SelectAction="Select" PopulateOnDemand="true"></asp:TreeNode> </Nodes> |
If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!
We set the initial Value of the TreeNode to the C drive, but we will change this on Page Load:
| Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load TreeView1.Nodes(0).Value = Server.MapPath("media") End Sub |
And we also add the following method to populate the TreeView:
| Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As TreeNodeEventArgs) If IsCallback = True Then End SubIf e.Node.ChildNodes.Count = 0 Then End IfLoadChildNode(e.Node) End IfPrivate Sub LoadChildNode(ByVal node As TreeNode) Dim directory As DirectoryInfo = Nothing End Sub directory = New DirectoryInfo(node.Value) For Each subtree As DirectoryInfo In directory.GetDirectories() Dim subNode As New TreeNode(subtree.Name) Next subtreesubNode.Value = subtree.FullName Try If subtree.GetDirectories().Length > 0 Or subtree.GetFiles().Length > 0 Then Catch ex As ExceptionsubNode.SelectAction = TreeNodeSelectAction.SelectExpand End IfsubNode.PopulateOnDemand = True subNode.NavigateUrl = "#" End Try node.ChildNodes.Add(subNode) For Each fi As FileInfo In directory.GetFiles() Dim subNode As New TreeNode(fi.Name) Next finode.ChildNodes.Add(subNode) subNode.NavigateUrl = "media/" & fi.Name.ToString() 'subNode.NavigateUrl = Server.MapPath(fi.Name.ToString()); |
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!
Here, we are loading every file and folder into the TreeView. Visual Studio will take care of the PostBack links for folders, but for files (ChildNodes), we assign the NavigateUrl ourselves. Note that the comment can be used instead to provide the full URL when on a Web Server.
Now if we run this web application, we should be able to click on the files listed to open them directly from our browser.
The entire code-behind looks something like this:
| Imports System.IO Partial Class _Default Inherits System.Web.UI.Page End Class Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load TreeView1.Nodes(0).Value = Server.MapPath("media") End SubProtected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As TreeNodeEventArgs) If IsCallback = True Then End SubIf e.Node.ChildNodes.Count = 0 Then End IfLoadChildNode(e.Node) End IfPrivate Sub LoadChildNode(ByVal node As TreeNode) Dim directory As DirectoryInfo = Nothing End Subdirectory = New DirectoryInfo(node.Value) For Each subtree As DirectoryInfo In directory.GetDirectories() Dim subNode As New TreeNode(subtree.Name) Next subtreesubNode.Value = subtree.FullName Try If subtree.GetDirectories().Length > 0 Or subtree.GetFiles().Length > 0 Then Catch ex As ExceptionsubNode.SelectAction = TreeNodeSelectAction.SelectExpand End IfsubNode.PopulateOnDemand = True subNode.NavigateUrl = "#" End Try node.ChildNodes.Add(subNode) For Each fi As FileInfo In directory.GetFiles() Dim subNode As New TreeNode(fi.Name) Next finode.ChildNodes.Add(subNode) subNode.NavigateUrl = "media/" & fi.Name.ToString() 'subNode.NavigateUrl = Server.MapPath(fi.Name.ToString()); |
No comments:
Post a Comment