asp.net 创建一个自定义下拉列表,用于选择文件夹的移动位置

djp7away  于 2024-01-09  发布在  .NET
关注(0)|答案(1)|浏览(147)

我需要做一个目录列表,其中包含所有可能的目录文件夹可以移动到。
列表的结构必须如下所示:

Main directory
    Sub directory
       Sub Sub directory
          ect directory
Main directory
    Sub directory
       Sub Sub directory
          ect directory
Main directory
    Sub directory

字符串
目录和子目录的数量不能硬编码,因为它会发生变化。目录列表中的每个目录都必须是可选择的值
这是我当前的代码,它将整个文件路径放入到目录列表中

string[] dirs = Directory.GetDirectories(pathToFollowForMove, "*", SearchOption.AllDirectories);
ddlFolders.Items.Clear();

foreach (string dir in dirs)
{
    int dirLength = dir.IndexOf(@"\U\");
    string listItem = dir.Substring(dirLength);

    if (!listItem.Contains(@"\bin"))
    {
        ddlFolders.Items.Insert(0, listItem);
    }
}


如何获取所选值:

string newDirectory = ddlFolders.SelectedValue.ToString();


列表的html代码:

<asp:DropDownList ID="ddlFolders" runat="server" ></asp:DropDownList>

pwuypxnk

pwuypxnk1#

我会考虑使用树视图。毕竟,您的问题是选择文件夹,而文件夹是树层次结构。
我建议使用一个文件夹来选择一些文件夹不会给用户给予任何类似文件夹结构的感觉。
非常有趣的是,树视图支持“按需”加载。这意味着你可以提供树视图你的根文件夹(c:)的硬盘驱动器,但加载时间将是即时的(因为子文件夹需要父文件夹被扩展,这样的子文件夹加载只发生在扩展一个给定的父文件夹)。
事实上,这样的代码甚至不是递归的,您只需要从某个“根”文件夹开始就可以了。
所以,说这个简单的标记:

<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        <br />
        <h3>Select folders</h3>

        <asp:TreeView ID="TreeView1" runat="server" ImageSet="XPFileExplorer"
            NodeIndent="15" OnTreeNodeExpanded="TreeView1_TreeNodeExpanded"  >
            <HoverNodeStyle Font-Underline="True" ForeColor="#6666AA" />
            <NodeStyle Font-Names="Tahoma" Font-Size="8pt" ForeColor="Black"
                HorizontalPadding="2px" NodeSpacing="0px" VerticalPadding="2px" />
            <ParentNodeStyle Font-Bold="False" />
            <SelectedNodeStyle BackColor="#B5B5B5" Font-Underline="False"
                HorizontalPadding="0px" VerticalPadding="0px" />
        </asp:TreeView>

字符串
后面的代码也很简单:

string sRoot = @"C:\Users\Kalla\source\repos";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Label1.Text = sRoot;

            DirectoryInfo[] MyFolders;
            MyFolders = GetFolders(sRoot);
            LoadTreeFiles(sRoot, MyFolders, "", null);
        }
    }

    public DirectoryInfo[] GetFolders(string sRoot)

    {
        DirectoryInfo MyDir = new DirectoryInfo(sRoot);
        DirectoryInfo[] cResultFolder; 
        cResultFolder = MyDir.GetDirectories("*.*", SearchOption.TopDirectoryOnly);

        return cResultFolder;
    }

    public void LoadTreeFiles(string fRootL,
                            DirectoryInfo[] lParent,
                            string sParentID,
                            TreeNode tTreeNode)
    {
        // get all folders (if any)
        foreach (DirectoryInfo sFolder in lParent)
        {
            TreeNode child = new TreeNode();
            child.Text = sFolder.Name;
            child.Value = sFolder.FullName;
            child.Expanded = false;
            child.PopulateOnDemand = true;
            child.ShowCheckBox = true;

            if (sParentID == "")
                TreeView1.Nodes.Add(child);
            else
                tTreeNode.ChildNodes.Add(child);
        }
    }

    protected void TreeView1_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
    {
        TreeNode child = e.Node;
        DirectoryInfo[] dtChild = GetFolders(child.Value);

        LoadTreeFiles(child.Value, dtChild, child.Text, child);

    }


结果是这样的:
x1c 0d1x的数据
现在,可以预先填充所有文件夹,但如上所示,通过按需加载,然后子文件夹不会加载,也不会遍历,直到您展开给定的文件夹。正如所指出的,这意味着性能应该接近即时,即使您从硬盘驱动器的根启动(因此,这种方法可以支持大量的文件夹和文件)。
这是一个简单的事情,然后循环/显示选定的文件夹列表后,以上与TreeView. numberedNodes集合。
如果这只是为了选择几个文件夹,那么我会考虑为文件夹使用不同的图像,比如说:



所以,上面是一个很好的起点,在一些难以显示许多文件夹的地方,我们看到使用一个简单的树视图可以轻松地显示和选择文件夹。

相关问题