0

I have an asp.net treeview (acting as a remote file and folder browser). When a node is selected all child nodes are automatically selected. This works fine (c# code below).

When any child is checked/unchecked I want all related parents to be checked/unchecked as well. I cannot figure this out. I want to use c# to do this.

-item1

------child1

------child2

  --child2.1
  --child2.2

------child3

Example 1 - if child 2.2 had its checkbox checked then child 2 and item1 will be checked automatically using c# code behind

Example 2 - if item1, child 2 , child 2.1 and child 2.2 were checked and if the user were to uncheck child 2.2 then item1, child 2 would remain checked as child 2.1 is still checked

thanks Damo

below is my code that checks all children of a checked item and works fine.

/// <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つの機能を持っています。1 つ目はすべての子をチェックし、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 に答える