title: Asp.Net TreeView with checkBox selection works only on first page Load.
body: <p>I am using <code>TreeView</code> with <code>ShowCheckBoxes="All"</code>. What I want to achieve with below code is that when a user click on <code>Parent node</code> all the <code>child nodes</code> get <code>selected</code>. : </p>
  
  <p><a href="https://i.stack.imgur.com/r0oX6.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/r0oX6.jpg" alt="enter image description here"></a></p>
  
  <p>This code works only on first page load. E.g. I click on "<code>Fruits</code>" and all the <code>child nodes</code> get selected and when I click again then all the <code>child nodes</code> get deselected. So far so good. But when I click on the  "<code>Fruits</code>" again then the <code>child nodes</code> don't get selected. I think the problem is somewhere in my <code>jquery</code> code.</p>
  
  <p><strong>ascx file:</strong></p>
  
  <pre><code>&lt;asp:TreeView ID="TreeView1" runat="server" ShowCheckBoxes="All"&gt;&lt;/asp:TreeView&gt;
  </code></pre>
  
  <p><strong>code behind:</strong></p>
  
  <pre><code> if (!IsPostBack)
              {
                  TreeView1.Nodes.Add(new TreeNode("Fruits", "Fruits"));
                  TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("Mango", "Mango"));
                  TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("Apple", "Apple"));
                  TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("Pineapple", "Pineapple"));
                  TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("Orange", "Orange"));
                  TreeView1.Nodes[0].ChildNodes.Add(new TreeNode("Grapes", "Grapes"));
  
                  TreeView1.Nodes.Add(new TreeNode("Vegetables", "Vegetables"));
                  TreeView1.Nodes[1].ChildNodes.Add(new TreeNode("Carrot", "Carrot"));
                  TreeView1.Nodes[1].ChildNodes.Add(new TreeNode("Cauliflower", "Cauliflower"));
                  TreeView1.Nodes[1].ChildNodes.Add(new TreeNode("Potato", "Potato"));
                  TreeView1.Nodes[1].ChildNodes.Add(new TreeNode("Tomato", "Tomato"));
                  TreeView1.Nodes[1].ChildNodes.Add(new TreeNode("Onion", "Onion"));
  
              }
  </code></pre>
  
  <p><strong>ascx file:</strong></p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  
          $(document).ready(function() {
  
              $("[id*=TreeView1] input[type=checkbox]").bind("click", function () {
                  var table = $(this).closest("table");
                  if (table.next().length &gt; 0 &amp;&amp; table.next()[0].tagName == "DIV") {
                      //Is Parent CheckBox
                      var childDiv = table.next();
                      var isChecked = $(this).is(":checked");
                      $("input[type=checkbox]", childDiv).each(function () {
                          if (isChecked) {
                              $(this).attr("checked", "checked");
                          } else {
                              $(this).removeAttr("checked");
                          }
                      });
                  } else {
                      //Is Child CheckBox
                      var parentDIV = $(this).closest("DIV");
                      if ($("input[type=checkbox]", parentDIV).length == $("input[type=checkbox]:checked", parentDIV).length) {
                          $("input[type=checkbox]", parentDIV.prev()).attr("checked", "checked");
                      } else {
                          $("input[type=checkbox]", parentDIV.prev()).removeAttr("checked");
                      }
                  }
              });
  
  
          });
  </code></pre>