Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8412039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:34:34+00:00 2026-06-10T00:34:34+00:00

I have an asp.net treeview (acting as a remote file and folder browser). When

  • 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;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T00:34:35+00:00Added an answer on June 10, 2026 at 12:34 am

    I figured this out

    I have 2 functions as follows . The first checks all the children and the second checks all the parents.
    Hope this helps someone.
    Damo

    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)
                {
    
    
                }
    
    
    
            }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ASP.Net TreeView Control with Checkboxes along Child nodes. I want to
I've used the following js code to expand/collapse all nodes of an ASP.Net TreeView
i have a databound treeview in asp.net. how can i expand the root node
I have a requirement to create a multi level treeview in ASP.Net (with VB)
I have listed data for ASP.net control TreeView. I wanted to disable a particular
I am trying to create a treeview dynamically using c# and asp.net. I have
I have an ASP.NET MVC application that has a jQuery Treeview and a jQuery
I do have a asp:TreeView control on my Asp.net page and when I render
I have a question regarding the ASP.NET 3.5 Treeview and Treenodes. I'd like to
I using TreeView with ShowCheckBoxes=All in an ASP.NET 3.5 web application and for some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.