title: check parents of child
body: <p>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).</p>
  
  <p>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. </p>
  
  <p>-item1</p>
  
  <p>------child1</p>
  
  <p>------child2</p>
  
  <pre><code>  --child2.1
    --child2.2
  </code></pre>
  
  <p>------child3</p>
  
  <p>Example 1 - if child 2.2 had its checkbox checked then child 2 and item1 will be checked automatically using c# code behind</p>
  
  <p>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</p>
  
  <p>thanks
  Damo</p>
  
  <p>below is my code that checks all children of a checked item and works fine.</p>
  
  <pre><code>/// &lt;summary&gt;
          /// Checks or unchecks child nodes when a parent node is checked or unchecked.
          /// &lt;/summary&gt;
          /// &lt;param name="sender"&gt;&lt;/param&gt;
          /// &lt;param name="e"&gt;&lt;/param&gt;
  
          protected void OnTreeNodeCheckChanged(object sender, TreeNodeEventArgs e)
          {
              // Determine if checked Node is a root node.
              if (e.Node.ChildNodes.Count &gt; 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);
  
              }
          }
  
          /// &lt;summary&gt;
          /// Recursively checks or unchecks all child nodes for a given TreeNode.
          /// &lt;/summary&gt;
          /// &lt;param name="node"&gt;TreeNode to check or uncheck.&lt;/param&gt;
          /// &lt;param name="check"&gt;Desired value of TreeNode.Checked.&lt;/param&gt;
          private void ChangeChecked(TreeNode node, bool check)
          {
              // "Queue" up child nodes to be checked or unchecked.
              if (node.ChildNodes.Count &gt; 0)
              {
                  for (int i = 0; i &lt; node.ChildNodes.Count; i++)
                      ChangeChecked(node.ChildNodes[i], check);
              }
  
  
              node.Checked = check;
          }
  </code></pre>