ASP.NET 2.0 : Hierarchical datasources

 

 

While implementing a virtual file system for a customer I found myself in need of a custom hierarchical datasource. As usual, the MSDN website offered a good sample of such a class. Easy right?

Anything but easy as it turns out. The sample code is all wrong. I ended up reading IL code to figure out how the SiteMapDataSource works. Reading IL is not exactly my idea of easy.

Turns out the problem is in the way the HierarchicalDataSourceControl and HierarchicalDataSourceView interact. The DataSource control is supposed to be the workhorse of the two. The view is just a container for the results.
In the sample however, the converse is true. That set me off in the wrong direction...

The easiest thing to do is this:
  1. Create a node class that implement IHierarchyData
  2. Create a control derived from HierarchicalDataSourceControl.
  3. Create a specialized collection to hold your nodes and implement IHierarchicalEnumerable
  4. Implement a HierarchicalDataSourceView
  5. Override the GetHierarchicalView method in the HierarchicalDataSourceControl.
The first four steps are easy. Create the classes and implement the methods, pretty straight foreward. Using generics the collection just requires an implementation for IHierarchicalEnumerable.GetHierarchyData which can be as simple as a type cast since the node class implements IHierarchyData. I'll get back to implementing the nodes later.

Implementing the HierarchicalDataSourceView is not that difficult either. It's just a wrapper around your collection. Store a copy of your collection class and return it in the Select method. That's all.

Overriding the GetHierarchicalView method
This is the method that does the actual work in the HierarchicalDataSource. The role of the viewPath parameter is the key to making this work; use it like a query. If you're loading objects from a database like me, the viewpath should be the unique key to the object.
In the implementation use the viewPath to decide what to do:
  • If the viewPath is empty, use the root node of your hierarchy. If you want to include the root node in the hierarchy, return the root node itself. If you don't return it's children.
  • If viewPath is not empty, fetch the node pointed to by the viewPath and return it's children.
    This is crucial! Do not return the node itself, return it's children because when a view is requested for a node, it means the node was already returned in a previous call. If you return the node itself, the datasource will get stuck in an endless loop...
Implementing the nodes
The node implementation is nothing spectacular. Just make sure that the Path property returns something that you can process as a viewPath in your GetHierarchicalView implementation.
If you want to bind a TreeView to your HierarchicalDataSourceControl you can use the Type property to hint the icon for the node. For example, when your hierarchy contains files and folders consider returning the mime type of the file so you can display the appropriate icon in the tree.

I've compiled a simple ready-to-run sample (VS 2005) that uses a TreeView control to render the files in a given folder on disk. 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值