[ASP.NET] TreeView 找到關鍵noted 往上展開

如標題

參考網址

 

https://stackoverflow.com/questions/45

        /// <summary>
        /// 找到預設展開的項目
        /// </summary>
        /// <param name="aNodes"></param>
        /// <param name="txt"></param>
        private void FindNodeToExpand (TreeNodeCollection axNodes , string txt)
        {
            if (string.IsNullOrEmpty(txt))
            {
                return;
            }

            foreach (TreeNode iNode in axNodes )
            {
                if (iNode.Text.Contains(txt))
                {
                    iNode.Text = "<span class=\"primary-bg\">"+ iNode.Text + "</span>";
                    iNode.Expand();
                    ExpandParentNodes(iNode.Parent.ChildNodes);
                }
                FindNodeToExpand(iNode.ChildNodes , txt);         
            }
        }

        /// <summary>
        /// 展開所有父節點
        /// </summary>
        /// <param name="tnodes"></param>
        private void ExpandParentNodes(TreeNodeCollection tnodes)
        {
            TreeNode parentNode;
            foreach (TreeNode node in tnodes)
            {
                parentNode = node;
                if (parentNode.Parent != null)
                {
                    parentNode.Parent.Expand();
                    if (parentNode.Parent.Parent != null)//判斷父節點的父節點是否為空,如果已經達到根節點就是null
                    {
                        ExpandParentNodes(parentNode.Parent.Parent.ChildNodes);
                    }
                    else
                        break;//已經達到根節點,退出
                }
            }
        }

20503/how-do-you-get-the-root-node-or-the-first-level-node-of-the-selected-node-in-a-t

以上文章僅用紀錄資料使用.....