简体   繁体   English

在asp.net中填充treeview

[英]Populating treeview asp.net

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 主要问题:当文件位于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;
        }

    }
}

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. 因此,需要指出的几件事在您的问题中并不十分清楚。

1. 1。

You need to hook the TreeView1_TreeNodePopulate to your TreeView control. 您需要将TreeView1_TreeNodePopulate挂钩到TreeView控件。 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 否则此事件处理程序将永远不会被击中

2. 2。

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... 除了连接OnTreeNodePopulate事件外,您还需要从标记中添加至少一个节点并将其PopulateOnDemand属性设置为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... 添加此“根”节点的另一个原因是在这里避免IndexOutOfRangeExceptionNullReference异常...

TreeView1.Nodes[0].Value = Server.MapPath("~/Files");

Keeping all that in mind, it should work just fine 牢记所有这些,它应该可以正常工作

Edit based on comment 根据评论进行编辑

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. 发生这种情况是因为您在创建和添加节点时传递了url。 Basically I'd recommend not using Server.MapPath on page load, add the virtual server path only... 基本上,我建议不要在页面加载时使用Server.MapPath ,仅添加虚拟服务器路径...

TreeView1.Nodes[0].Value = "~/Files";

then use Server.MapPath when creating the DirectoryInfo object... 然后在创建DirectoryInfo对象时使用Server.MapPath ...

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... 最后,在“文件”迭代中,如下所示设置节点的NavigateUrl的属性...

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 (eg images, etc) 请注意,这类似于使用Web浏览器发出http请求,并且该请求将由IIS和ASP.NET管道处理...这意味着您将只能通过以下方式查看IIS可以处理的文件:默认值(例如图像等)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM