Disable unchecking child nodes when parent node is checked

3 posts, 0 answers
  1. Marja
    Marja avatar
    70 posts
    Member since:
    Nov 2013

    Posted 20 Mar 2014 Link to this post

    I have a treeview with checkboxes where selecting/checking a parent node also needs to selects/checks all its child nodes. This can easily be handled by setting CheckChildNodes="true".

    However, it is then still possible to unselect those childnodes. How can I prevent that?

    Best regards, Marja
  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 21 Mar 2014 in reply to Marja Link to this post

    Hi Marja,

    Please try the following JavaScript to achieve your scenario.

    JavaScript:
    <script type="text/javascript">
        function OnClientNodeChecking(sender, args) {
            //OnClientNodeChecking event of RadTreeView
            if (args.get_node()._hasChildren() == false) {
                if (args.get_node().get_parent().get_checked() == true) {
                    args.set_cancel(true);
                }
            }
        }
    </script>

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Marja
    Marja avatar
    70 posts
    Member since:
    Nov 2013

    Posted 21 Mar 2014 in reply to Princy Link to this post

    Hi Princy,

    Thank you for your help.

    Your answer was not exactly what I needed, but it put me on the right track. This is what finally worked for me:

    <script type="text/javascript">
            function UpdateAllChildren(nodes, checked) {
                var i;
                var test;
                for (i = 0; i < nodes.get_count() ; i++) {
                    if (checked) {
                        nodes.getNode(i).check();
                    }
                    else {
                        nodes.getNode(i).set_checked(false);
                    }
     
                    if (nodes.getNode(i).get_nodes().get_count() > 0) {
                        UpdateAllChildren(nodes.getNode(i).get_nodes(), checked);
                    }
                }
            }
     
            function OnClientNodeChecking(sender, args) {
                var node = args.get_node();
                if (node.get_parent() && node.get_parent().get_checked() == true) {
                    node.check();
                    args.set_cancel(true);
                }
            }
     
            function OnClientNodeChecked(sender, args) {
                var node = args.get_node();
                if (node.get_checked()) {
                    if (node.get_nodes().get_count() > 0) {
                        UpdateAllChildren(node.get_nodes(), true);
                    }
                }
            }
        </script>
Back to Top