0

我有一个 asp.net 树视图(充当远程文件和文件夹浏览器)。当一个节点被选中时,所有的子节点都会被自动选中。这很好用(下面的 c# 代码)。

当任何孩子被选中/取消选中时,我希望所有相关的父母也被选中/取消选中。我无法弄清楚这一点。我想使用 c# 来做到这一点。

-项目1

------孩子1

------孩子2

  --child2.1
  --child2.2

------孩子3

示例 1 - 如果 child 2.2 选中了其复选框,则 child 2 和 item1 将使用后面的 c# 代码自动检查

示例 2 - 如果选中了 item1、child 2、child 2.1 和 child 2.2,并且如果用户取消选中 child 2.2 那么 item1, child 2 将保持选中状态,因为 child 2.1 仍然处于选中状态

谢谢达摩

下面是我的代码,它检查检查项目的所有子项并且工作正常。

/// <summary>
        /// Checks or unchecks child nodes when a parent node is checked or unchecked.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        protected void OnTreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
        {
            // Determine if checked Node is a root node.
            if (e.Node.ChildNodes.Count > 0)
            {
                // Check or uncheck all of the child nodes based on status of parent node.
                if (e.Node.Checked)
                    ChangeChecked(e.Node, true);
                else
                    ChangeChecked(e.Node, false);

            }
        }

        /// <summary>
        /// Recursively checks or unchecks all child nodes for a given TreeNode.
        /// </summary>
        /// <param name="node">TreeNode to check or uncheck.</param>
        /// <param name="check">Desired value of TreeNode.Checked.</param>
        private void ChangeChecked(TreeNode node, bool check)
        {
            // "Queue" up child nodes to be checked or unchecked.
            if (node.ChildNodes.Count > 0)
            {
                for (int i = 0; i < node.ChildNodes.Count; i++)
                    ChangeChecked(node.ChildNodes[i], check);
            }


            node.Checked = check;
        }
4

1 回答 1

0

我想通了

我有 2 个功能如下。第一个检查所有孩子,第二个检查所有父母。希望这可以帮助某人。达摩

ChangeCheckedChildren(e.Node, e.Node.Checked);
ChangeCheckedParents(e.Node, e.Node.Checked);



 /// <summary>
        /// Recursively checks or unchecks all child nodes for a given TreeNode.
        /// </summary>
        /// <param name="node">TreeNode to check or uncheck.</param>
        /// <param name="check">Desired value of TreeNode.Checked.</param>
        private void ChangeCheckedChildren(TreeNode node, bool check)
        {

            try
            {
            // "Queue" up child nodes to be checked or unchecked.
            if (node.ChildNodes.Count > 0)
            {
                for (int i = 0; i < node.ChildNodes.Count; i++)
                    ChangeCheckedChildren(node.ChildNodes[i], check);
            }
            node.Checked = check;
            }


            catch (Exception ex)
            {

            }

        }

        /// <summary>
        /// Recursively checks or unchecks all parent nodes for a given TreeNode.
        /// </summary>
        /// <param name="node">TreeNode to check or uncheck.</param>
        /// <param name="check">Desired value of TreeNode.Checked.</param>
        private void ChangeCheckedParents(TreeNode node, bool check)
        {
            try
            {

                if (node.Parent == null)  // if we are at the root node
                {
                    if (node.ChildNodes.Count > 0)
                    {
                        for (int i = 0; i < node.ChildNodes.Count; i++)
                        {
                            if (node.ChildNodes[i].Checked == true)
                            {
                                node.Checked = true;
                                return;
                            }
                        }
                        node.Checked = false;
                    }
                    else
                    {
                        node.Checked = check;
                    }

                }
            else
            {
                // Check all parents if the user is checking
                if (check == true)
                {
                    node.Checked = check;
                    ChangeCheckedParents(node.Parent, check);
                }

                else
                {
                    // Do not uncheck a parent if any of its other children or their children are checked
                    if (node.ChildNodes.Count > 0)
                    {
                        // Default to not check and check if required
                        node.Checked = false;
                        for (int i = 0; i < node.ChildNodes.Count; i++)
                        {
                            if (node.ChildNodes[i].Checked == true)
                            {
                                node.Checked = true;
                            }
                        }                            
                            ChangeCheckedParents(node.Parent, check);
                    }
                    else
                    {
                        node.Checked = check;
                        ChangeCheckedParents(node.Parent, check);
                    }
                }
            }
            }

            catch (Exception ex)
            {


            }



        }
于 2012-08-21T19:02:58.577 回答