Click here to Skip to main content
15,907,395 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello

I have a tree view the will be populated by folder & files by TreeNodePopulate function. When I run the code tree view will be populated correctly but when I want to filter tree view by function (FindNodeByValue) I have problem, it seems TreeNodeCollection just has 1 node that I had made handy in the first of code (Page_Load ) and other nodes that will be produced by TreeNodePopulate function  not added in TreeNodeCollection, please help how I can improve code & solve it.

The code is like below:


C#
<pre>protected void Page_Load(object sender, EventArgs e)
{
    TreeView1.Nodes[0].Value = Server.MapPath("Document/Detail Engineering");


}


protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
    if (IsCallback == true)
    {
        if (e.Node.ChildNodes.Count == 0)
        {
            LoadChildNode(e.Node);
        }
    }
}

private void LoadChildNode(TreeNode node)
{
    DirectoryInfo directory = null;
    directory = new DirectoryInfo(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 (Exception ex)
        {

        }
        node.ChildNodes.Add(subNode);
    }
    foreach (FileInfo fi in directory.GetFiles())
    {
        TreeNode subNode = new TreeNode(fi.Name);
        node.ChildNodes.Add(subNode);
        subNode.NavigateUrl = "media/" + fi.Name.ToString();
        //subNode.NavigateUrl = Server.MapPath(fi.Name.ToString());
    }
}



private IEnumerable<TreeNode> FindNodeByValue(TreeNodeCollection nodes, string searchstring)
{

    foreach (TreeNode node in nodes)
    {
        if (node.Value.IndexOf(searchstring,
              StringComparison.CurrentCultureIgnoreCase) >= 0)
            yield return node;
        else
        {
            foreach (var subNode in FindNodeByValue(node.ChildNodes, searchstring))
                yield return subNode;
        }
    }
    //TreeView1.Nodes.Clear();
}
protected void Button1_Click(object sender, EventArgs e)
{

    var query = FindNodeByValue(TreeView1.Nodes, fieldFilterTxtBx.Text);
    if (query != null)
    {
        //TreeView1.Nodes[0].Expand();
        //TreeView1.Nodes.Clear();
        foreach (TreeNode node in query.ToList())
        {
            TreeView2.Nodes.Add(node);

        }



        TreeView2.Visible = true;
        TreeView2.ExpandAll();
    }

    else
    {

        Label1.Text = "No file found";

    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900