This space is available to any ComponentArt employee to write about anything.

TreeView and Web Services


In my previous post, I talked about the web service functionality that's common to all ComponentArt navigation controls. I focused on common features that are present in many different controls, especially in this case, but this approach was unfair to one control. With its ability to load chunks of data on demand, TreeView is the odd man out, supporting everything that other navigation controls do, but adding its own unique twist.

Like the other navigation controls, TreeView has control-level WebService and WebServiceMethod properties. These determine which ASP.NET AJAX registered web service will be called, and which method of that service, when nodes are retrieved. For the initial data load, the behaviour is identical to Menu, TabStrip or NavBar. What TreeView can do that others can not is to only load some nodes initially, letting user actions determine what else gets loaded and when. Essentially, TreeView is able to operate in load-on-demand mode with web services.

To return load-on-demand nodes (parent nodes with no child nodes pre-loaded), simply set their UseWebService property to true. This will notify TreeView to consider these nodes to be parent nodes, and to call the web service again when they are expanded, to load their child nodes. When that happens, the same WebServiceMethod will be called, but with a twist: the TreeViewWebServiceRequest object that is passed in will have its Node property set to the node that requires data. Based on the Text, Value or ID of this node, appropriate new nodes can be created and returned, as they would normally, from the web service method. Note that these child nodes can also have their UseWebService property set to true.

After a web service call completes, the client-side event also exposes the node for which the call was made, in addition to the customData property common to all navigation controls. Here's an overview of the client-side TreeViewNodeWebServiceCompleteEventArgs class:

function TreeView1_webServiceComplete(sender, eventArgs)
{
  var node = eventArgs.get_node();
  
  if(node)
  {
    alert('Completed web service call for node ' + node.get_text());
  }
  else
  {
    alert('Completed web service call for top level data.');
  }
  
  var customData = eventArgs.get_customData();
  
  if(customData)
  {
    alert('Web service call returned custom data: ' + customData);
  }
}

You can see an example of this functionality online with the TreeView Web Service Load-on-demand sample. Please note that this functionality is fully available only as of Web.UI 2008.1 SP1, released on April 23rd 2008.

Summary

To recap, since TreeView can load node-specific partial data through web services (unlike other navigation controls, which can only load all the items at once), its related API is a superset of, say, that of Menu. TreeViewWebServiceRequest includes the Node property (null for initial, top-level loading), TreeViewNode has the UseWebService boolean, and the client-side TreeViewWebServiceCompleteEventArgs class includes a node property. These extensions enable efficient load-on-demand functionality built entirely on ASP.NET AJAX web services.

Share this post: email it! | bookmark it! | digg it! | reddit!

Posted by: Milos
Posted: Wednesday, April 23, 2008 10:49 AM


Comments

BJ said:

"... Web.UI 2008.1 SP1, released on April 23rd 2008". Can't seem to locate SP1
# April 23, 2008 8:23 PM

milos said:

BJ, it's the current 2008.1 build (version 2008.1.1110) available for download. The download page doesn't specify that it's a service pack release and not the original 2008.1. Apologies for the confusion.
# April 24, 2008 6:47 AM

Brian MacKay said:

This is definitely interesting techology and the strides you guys have made are most impressive. The big problem we've had with the treeview is getting persistance to work in a high-performance way out of the box. I'm hoping the recent versions have created a better way to deal with these problems. Here's the problem in a nutshell: Once the load-on-demand occurs, how does that data persist between postpacks? Well, the treeview builds an xml representation of its contents in a hidden field and sends that along with each postback/callback. Even for trees that only load-on-demand a few dozen items, this can become large. In our case we were dealing with dozens of nodes and it added 200k to the page - ouch. Here at Inntec, we tackled this problem in by re-writing the plumbing to store our persistance data in session state. This is more involved than it may sounds, involving code in three or four places plus some javascript and new hidden fields. This is painful and difficult to reproduce in an elegant way. Is there a better way with the recent improvements?
# April 24, 2008 8:30 AM

Michal Miszczyszyn said:

Hello, But what if I want to populate whole tree from webservice call. I have a list of TreeViewNodes which is a result of some search operation, and now I want to populate TreeView with those nodes? TreeViewCollection, TreeViewWebServiceResponse are not getting serialized properly using JSON. Any ideas?
# July 29, 2008 5:49 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Blogs On This Site
Thoughts on web user interfaces and component development.
Ramblings of a web control developer.
Web.UI news and more
Next weeks guest: A dog and a baby dog!
I'm in your base, killing your dudes.
Absurdity is it's own message.
Musings of an ex Java developer
im in ur page, hackin ur codez
ComponentArt in the Community
... and the program ran happily ever after.

This Blog