Setting the checked state of nodes loaded on demand

13 posts, 0 answers
  1. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 17 Feb 2012 Link to this post

    I have a treeview which I'm loading on demand via a webservice.

    My webservice sets attributes on the nodes to indicate a) whether or not to show the checkbox for that node and b) whether or not the checkbox should be checked.

    My treeview has 2 levels.

    When the WebService call succeeds, the following code is called to populate the tree...

    treeView.get_nodes().clear();
      
    if (treeNodesResults != null)
    {
        for (var i = 0; i < treeNodesResults.length; i++)
        {
            var node = new Telerik.Web.UI.RadTreeNode();
            node.set_text(treeNodesResults[i].Text);
            node.set_value(treeNodesResults[i].Value);
            node.set_expandMode(treeNodesResults[i].ExpandMode);
            node.set_imageUrl(treeNodesResults[i].ImageUrl);
      
            var checkable = false;
            var checkableAttribute = node.get_attributes().getAttribute("Checkable");
            if (checkableAttribute != null)
            {
                checkable = checkableAttribute == "true";
            }
            node.set_checkable(checkable);
      
            if (checkable)
            {
                var checked = false;
                var checkedAttribute = node.get_attributes().getAttribute("Checked") == "true";
                if(checkedAttribute != null)
                {
                    checked = checkedAttribute == "true";
                }
                node.set_checked(checked);
            }
      
            treeView.get_nodes().add(node);
        }
    }
    And, on the face of it, this appears to work.

    However, it seems that this code is only being called for the top-level nodes in the tree. None of the child nodes are being processed by this code. In fact, this code doesn't appear to be being called at all for anything other than the top-level nodes.

    Clearly, I'm missing something. Can anyone give me an idea about what it might be?

    --
    Stuart
  2. Bozhidar
    Admin
    Bozhidar avatar
    1102 posts

    Posted 20 Feb 2012 Link to this post

    Hi Stuart,

    Please preview the following articles explaining how to implement LoadOnDemand using web services:
    http://www.telerik.com/help/aspnet-ajax/treeview-load-on-demand-webservice.html 
    http://www.telerik.com/help/aspnet-ajax/treeview-load-on-demand-web-service-tutorial.html 
    http://demos.telerik.com/aspnet-ajax/treeview/examples/programming/loadondemandmodes/defaultcs.aspx

    And the following article demonstrates how you can use attributes to set different properties of the node:
    http://www.telerik.com/help/aspnet-ajax/treeview-data-binding-node-databound.html 
     
    Greetings,
    Bozhidar
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 20 Feb 2012 Link to this post

    Bozhidar,

    Thanks for your input. However...

    I know how to populate the tree using LoD from a webservice; I'm doing it. It works. What I can't do is set the checked state of nodes below the top-level nodes.

    I know how to use attributes on the object to carry out custom actions. The original example I posted shows me doing that. I'm using attributes on the RadTreeNodeData object because it doesn't know about the Checable or Checked properties that the RadTreeNode has.

    As my original post pointed out, the problem I'm having is that the code used to populate the nodes is obnly apparently called when the top-level nodes are populated; it isn't called for the child nodes of one of these top-level nodes.

    I can't use the server-side databound event 'cos the data is coming from a webservice and I don't seem to bet able to get at the custom attributes of the RadTreeNodeData object in the treeview's client-side databand event (get_dataItem() seems to have attributes but I'll be damned if I can get at any values on it!)
  5. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 20 Feb 2012 Link to this post

    Never mind.

    I'm sorted.

    --
    Stuart
  6. msigman
    msigman avatar
    141 posts
    Member since:
    Apr 2008

    Posted 14 Mar 2012 Link to this post

    Stuart,

    I'm running into the same issue.  Would you mind sharing how you resolved it?

    Thanks
    Matt
  7. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 14 Mar 2012 Link to this post

    Matt,

    Can you be a bit more specific about the nature of your problem?

    I ask 'cos, loath as I am to admit it, mine turned out to be doing something dumb.

    -- 
    Stuart
  8. msigman
    msigman avatar
    141 posts
    Member since:
    Apr 2008

    Posted 14 Mar 2012 Link to this post

    I'm just wanting to set checkbox state (checked/unchecked) when they are returned from a web service. Like you, I also tried the server-side events without any luck. Matt
  9. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 14 Mar 2012 Link to this post

    OK,

    In short, I populate the TreeNodeData object in the webservice with a copy of custom attributes to indicate (a) whether or not the node should show the checkbox and (b) whether or not the checkbox should be checked.

    The trick was to access the custom attributes in the correct handler in the JavaScript code after the event.

    What I did was wire-up the OnClientNodePopulated event to this function...
    function ClientNodePopulated(sender, e)
    {
      var nodes = e.get_node().get_nodes();
      for(var i=0; i<nodes.get_count(); i++)
      {
        var node = nodes.getNode(i);
         
        var checkable  = false;
        var checkableAttribute = node.get_attributes().getAttribute("Checkable");
        if(checkableAttribute != null)
        {
          checkable = checkableAttribute == "true";
        }
     
        node.set_checkable(checkable);
     
        if(checkable)
        {
          var checked  = false;
          var checkedAttribute = node.get_attributes().getAttribute("Checked");
          if(checkedAttribute != null)
          {
            checked = checkedAttribute == "true";
          }
          node.set_checked(checked);
        }
      }
    }

    Obviously, "Checkable" and "Checked" are custom attributes populated in the webservice using code like this...
    foreach(DataItem item in ListOfItemsToAdd)
    {
      RadTreeNodeData = nodeData = new RadTreeNodeData();
      // populate the standard stuff, then...
      if(checkableTest)
      {
        nodeData["Checkable"] = checkableValue;
      }
     
      if(checkedTest)
      {
        nodeData["Checked"] = checkedValue;
      }
    }

    Hope there's enough there for you to get going.

    -- 
    Stuart

  10. msigman
    msigman avatar
    141 posts
    Member since:
    Apr 2008

    Posted 15 Mar 2012 Link to this post

    Stuart,

    Great work!  It worked perfectly.  I just had to make one small change to your code (looks like a simple typo):

    nodeData.Attributes["Checked"] = checkedValue;
  11. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 15 Mar 2012 Link to this post

    Matt,

    Happy to help.

    Please remember click on "Mark as Answetr" on my post.

    --
    Stuart
  12. msigman
    msigman avatar
    141 posts
    Member since:
    Apr 2008

    Posted 15 Mar 2012 Link to this post

    I'd be happy to, but you are the original poster of this question and so I can't!  I can create a new thread if you'd like to get the MVP points on this one since you were very helpful.
  13. Stuart Hemming
    Stuart Hemming avatar
    1622 posts
    Member since:
    Jul 2004

    Posted 15 Mar 2012 Link to this post

    Thanks Matt,

    Don't worry about it. I imagine they would look unkindly on repeated posts.

    Take care.

    --
    Stuart
  14. msigman
    msigman avatar
    141 posts
    Member since:
    Apr 2008

    Posted 15 Mar 2012 Link to this post

    Maybe they can mark your post as the Answer, since it does in fact resolve the original question.

    Thanks again for the help,
    Matt
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017