This is a migrated thread and some comments may be shown as answers.

Setting the checked state of nodes loaded on demand

12 Answers 132 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Stuart Hemming
Top achievements
Rank 2
Stuart Hemming asked on 17 Feb 2012, 03:59 PM
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

12 Answers, 1 is accepted

Sort by
0
Bozhidar
Telerik team
answered on 20 Feb 2012, 12:04 PM
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 >>
0
Stuart Hemming
Top achievements
Rank 2
answered on 20 Feb 2012, 12:54 PM
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!)
0
Stuart Hemming
Top achievements
Rank 2
answered on 20 Feb 2012, 01:56 PM
Never mind.

I'm sorted.

--
Stuart
0
msigman
Top achievements
Rank 2
answered on 14 Mar 2012, 10:19 PM
Stuart,

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

Thanks
Matt
0
Stuart Hemming
Top achievements
Rank 2
answered on 15 Mar 2012, 01:37 AM
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
0
msigman
Top achievements
Rank 2
answered on 15 Mar 2012, 01:43 AM
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
0
Stuart Hemming
Top achievements
Rank 2
answered on 15 Mar 2012, 02:22 AM
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

0
msigman
Top achievements
Rank 2
answered on 15 Mar 2012, 04:53 PM
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;
0
Stuart Hemming
Top achievements
Rank 2
answered on 15 Mar 2012, 05:13 PM
Matt,

Happy to help.

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

--
Stuart
0
msigman
Top achievements
Rank 2
answered on 15 Mar 2012, 05:14 PM
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.
0
Stuart Hemming
Top achievements
Rank 2
answered on 15 Mar 2012, 05:40 PM
Thanks Matt,

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

Take care.

--
Stuart
0
msigman
Top achievements
Rank 2
answered on 15 Mar 2012, 05:42 PM
Maybe they can mark your post as the Answer, since it does in fact resolve the original question.

Thanks again for the help,
Matt
Tags
TreeView
Asked by
Stuart Hemming
Top achievements
Rank 2
Answers by
Bozhidar
Telerik team
Stuart Hemming
Top achievements
Rank 2
msigman
Top achievements
Rank 2
Share this question
or