locked
ASP.NET treeview - select all parent node when child node is selected RRS feed

  • Question

  • User1762461883 posted

    I have one ASP.NET treeview control, if I select any child node then associated all parent nodes should get selected automatically. If I uncheck any child node then the parent node should get unselected only if there are no other siblings get selected.

    <div class="treenode-content">
      <asp:TreeView ID="treeEditRes" runat="server" ShowCheckBoxes="All" onclick="client_OnTreeNodeChecked(event);" >                   
      </asp:TreeView>
    </div>
    function client_OnTreeNodeChecked(event) {
      var obj;
      if (window.event == undefined) {
        obj = event.target;
      } else {
        obj = window.event.srcElement;
      }
      var treeNodeFound = false;
      var checkedState;
      if (obj.tagName == "INPUT" && obj.type == "checkbox") {
        var treeNode = obj;
        checkedState = treeNode.checked;
        do {
          obj = obj.parentElement;
        } while (obj.tagName != "TABLE")
        var par = $(obj.parentElement);
        while (par.parent().attr("class") != "treenode-content") {
          var currPar = $(par).siblings(0).find("input[type='checkbox']").first();
          if (checkedState) {
            currPar.prop("checked", true);
          } else {
            if (currPar.prop("checked")) {
              currPar.prop("checked", false);
            } else
              break
          }
          par = par.parent();
        }
      }
    }

    I have written the above function to check all parent nodes if a child node is selected, but it won't work if any siblings node is selected it will directly check/uncheck the parent node, it has to check for the siblings node and if no nodes are selected then uncheck the parent(like below example)

    Thursday, April 5, 2018 7:29 AM

All replies