OnClientNodeChecking

2 posts, 0 answers
  1. Christian Pardun
    Christian Pardun avatar
    112 posts
    Member since:
    May 2010

    Posted 19 Mar 2013 Link to this post

    Hi,

    I have an TreeView with CheckChildNodes=true. Also I have add the client event ClientNodeChecking.

    I expect if I click a node the client event ClientNodeChecking is fired for the clicked node and for all child nodes. Unfortunaltly it is only fired for the clicked node! Why and how can I solved this?

    Background is that i must check the count of all checked nodes and only allowed a maximum of 10.

    function FnTreeView_ClientNodeChecking(sender, args)
    {
        if (!args.get_node().get_checked())
        {
            var selectedNodesCount = sender.get_checkedNodes().length;
     
            if (selectedNodesCount > 10)
            {
                args.set_cancel(true);
            }
        }
    }


    Thanks for help,

    Kind regards,
    Christian

  2. Bozhidar
    Admin
    Bozhidar avatar
    1102 posts

    Posted 20 Mar 2013 Link to this post

    Hello Christian,

    The OnClientNodeChecking event fires as a result of an user interaction. It's fired for the node that's being clicked with the mouse (or by pressing space). Since the check child nodes functionality uses javascript to manually check the child nodes after that, the event is not raised again. It the same as if you use the following code to check a node:
    $find("RadTreeView1").get_nodes().getNode(0).check()

    In your case you can use the following code to cancel the check if the resulting amount of checked nodes is bigger than 10:
    function OnClientNodeChecking(sender, args)
    {
        if (!args.get_node().get_checked())
        {
            var selectedNodesCount = sender.get_checkedNodes().length;
            var childNodes = args.get_node().get_allNodes();
            var childNodesCount = childNodes.length;
     
            for(var i = 0; i < childNodesCount; i++) {
                if (childNodes[i].get_checked()) {
                    selectedNodesCount--;
                }
            }
     
            selectedNodesCount += childNodesCount;
      
            if (selectedNodesCount > 10)
            {
                args.set_cancel(true);
            }
        }
    }

    If you want to check child nodes until the total number reaches 10, I suggest you implement this functionality manually, instead of using the CheckChildNodes feature.
     

    Kind regards,
    Bozhidar
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top