Download demo application - 6.00 KB
In my last blog Binding a TreeView control to a hierarchical data structure, Part I, I had describe how one can bind a TreeView control to a hierarchical data structure through recursion. Now I’m going to describe how you can bind a TreeView control to a hierarchical data structure on demand. Basically this technique is useful for very long complex hierarchical data structure.
Simply drag and Drop a TreeView control and handle its OnTreeNodePopulate event.
Target Hierarchical Data
Target hierarchical data that I've chosen for this demo is a folder's hierarchical structure.
Binding TreeView Control
The TreeView control for this demo has been bind in the Page_Load event through the BindTreeView method as-
In this method a TreeView node for the top root folder of given folder hierarchical structure has been created with the help of GetNode method.
This method is simply used to create a TreeView node. Each TreeView node created by this method takes folder name as a Text, folder's fully qualified path as a Value and a folder image. It takes DirectoryInfo as an argument.
Whenever any TreeView node is expanded first time, then this event gets fired. Note that this request is handled through an AJAX call by ASP.NET itself implicitly. Here all the immediate child nodes are attached ot its parent node. On subsequent expansion of that particular parent node, this event isn't gets fired. Simply all the child nodes are fetched from the cache.
You can bind TreeView control to a database hierarchical data source in a similar manner. Do let me know if you have any query or problem.