Reputation: 317
Is there a way of populating a treeview including the parent's sub folder? My code only can only view files on its parent folder but once it in a sub folder it won't open.
Main problem: I can't open a file when it's inside a sub folder of my MapPath
Here's mine, so far it only gets the parent node it doesn't get the parent's sub folder:
protected void Page_Load(object sender, EventArgs e)
{
TreeView1.Nodes[0].Value = Server.MapPath("~/Files");
}
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
if (e.Node.ChildNodes.Count == 0)
{
DirectoryInfo directory = null;
directory = new DirectoryInfo(e.Node.Value);
foreach (DirectoryInfo subtree in directory.GetDirectories())
{
TreeNode subNode = new TreeNode(subtree.Name);
subNode.Value = subtree.FullName;
try
{
if (subtree.GetDirectories().Length == 0 | subtree.GetFiles().Length == 0)
{
subNode.SelectAction = TreeNodeSelectAction.SelectExpand;
subNode.PopulateOnDemand = true;
subNode.NavigateUrl = "";
}
}
catch
{
}
e.Node.ChildNodes.Add(subNode);
}
foreach (FileInfo fi in directory.GetFiles())
{
TreeNode subNode = new TreeNode(fi.Name);
e.Node.ChildNodes.Add(subNode);
subNode.NavigateUrl = "Files/" + fi.Name;
}
}
}
Upvotes: 0
Views: 1933
Reputation: 14820
There's absolutely nothing wrong with your code. I've run a test it works like a charm. So, a few things to point out which are NOT exactly clear in your question.
You need to hook the TreeView1_TreeNodePopulate
to your TreeView
control. You can do that declaratively from the markup...
<asp:TreeView ID="TreeView1" runat="server" OnTreeNodePopulate="TreeView1_TreeNodePopulate">
or, imperatively from code behind...
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
TreeView1.TreeNodePopulate += TreeView1_TreeNodePopulate;
}
otherwise this event handler will never get hit
In addition to hooking up the OnTreeNodePopulate
event you need to add at least one node from the markup and set its PopulateOnDemand
property to true...
<Nodes>
<asp:TreeNode PopulateOnDemand="true" Text="Root"></asp:TreeNode>
</Nodes>
if you don't set this property this event will never get triggered. Another reason to add this "root" node is to avoid an IndexOutOfRangeException
or NullReference
exception here...
TreeView1.Nodes[0].Value = Server.MapPath("~/Files");
Keeping all that in mind, it should work just fine
I didn't noticed the bit where you said you want to open the files when the tree node is clicked. And that happens because you are passing the url when creating and adding the nodes. Basically I'd recommend not using Server.MapPath
on page load, add the virtual server path only...
TreeView1.Nodes[0].Value = "~/Files";
then use Server.MapPath
when creating the DirectoryInfo
object...
directory = new DirectoryInfo(Server.MapPath(e.Node.Value));
and set the value of the tree node (inside the "directories" iteration) by appending a parent value's...
subNode.Value = string.Format("{0}/{1}", e.Node.Value, subtree.Name);
and finally, within the "files" iteration, set the NavigateUrl
's property of the node like below...
subNode.NavigateUrl = string.Format("{0}/{1}", e.Node.Value, fi.Name);
That should give you a proper link in your file nodes. Notice, that this is similar to issuing an http request using a web browser and the request will be handled by IIS and the ASP.NET pipeline...which means that you will only be able to see files that can be handled by IIS by default (e.g. images, etc)
Upvotes: 1