在treeview控件asp.net中的无限子节点和节点中搜索

polhcujo  于 2021-09-29  发布在  Java
关注(0)|答案(0)|浏览(158)

我试图在asp.net treeview控件中实现搜索,但我的算法只搜索一个子节点,但我有grand child内部子节点,因为我在树视图控件中获取系统文件和文件夹,所以请您帮助我实现从asp.net或javascript搜索多个节点的功能。这是我的密码

TreeNode SearchNode(TreeNode node, string searchText = null)
    {
        if (node.Text == searchText) return node;

        TreeNode tn = null;
        foreach (TreeNode childNode in node.ChildNodes)
        {
            tn = SearchNode(childNode);
            if (tn != null) break;
        }

        if (tn != null) node.Expand();
        return tn;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(Txtfind.Text))
        {
            TreeNode searchedNode = null;
            foreach (TreeNode node in TreeView1.Nodes)
            {
                searchedNode = SearchNode(node, Txtfind.Text);
                if (searchedNode == null)
                {
                    foreach (TreeNode childNode in node.ChildNodes)
                    {
                        searchedNode = SearchNode(childNode, Txtfind.Text);
                        if (searchedNode != null)
                            goto Here;
                    }
                }
                else
                {
                    break;
                }
            }
            Here:
            if (searchedNode != null)
            {
                searchedNode.Select();
                TreeView1.ExpandAll();
                Txtfind.Text = "";
            }
            else
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "Not Found", "alert('Node " + Txtfind.Text + " not found');", true);
            }
        }
    }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题