Question:
Creating a custom drop-down list for selecting where to move a folder

Solution:

Use a tree view here. Your question is on selecting folders, and if folders are in a tree hierarchy structure then you should use tree view. 


Tree view also allows for "on-demand" loading. This means that even if you gave the tree view your hard drive's root folder (C:), the load time would still be instantaneous because subfolders need their parent folders to be expanded, and this loading of subfolders only happens when a parent folder is expanded.


Such code only needs to start at some "root" folder to function; it is not even recursive.



        <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>

The below are the markup code: 

    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);


    }

And the result is this:

Although it is possible to pre-fill every folder, loading on demand prevents subfolders from loading or being accessed until a folder is expanded. As previously mentioned, this means that performance should be almost instantaneous, even if you started from the hard drive's root (many folders and files can be supported using this method).


Then, using TreeView to loop through or display the list of chosen folders is a straightforward process.collection of checked nodes.


I would think about using a different image for the folders if this is only for a few folders, something like this:


Therefore, the above is a good place to start. We can see that utilizing a simple tree view can display and allow selecting of folders with ease, in place of some dropdowns that make it difficult to display many folders.


Suggested blogs:

>>Step by Step guide to Deploy Terraform in Azure using GitHub Actions

>>Testing react components using Hooks and Mocks

>>How to solve Static Files not being found in Django?

>>How to format date-time input in django?

>>How to make a code executed after an entry is inserted, removed or updated?

>>How to insert new data into the table in Django?

>>Disabling default ordering in Django admin panel

>>How to migrate `DateTimeField` to `DateField` in Django?

>>Why VSCode indicate an error on using {{ }}?

>>Fix the python-decouple issue in Django

>>How to Set up the local environment for Angular development?

>>How to solve encoding issue when writing to a text file, with Python?


Ritu Singh

Ritu Singh

Submit
0 Answers