Project Description
This ASP.NET 2.0 hierarchical data source control aggregates tabular data sources and links them with relations to form a hierarchical data source that can be used with hierarchical controls such as the Menu or TreeView.

Tabular data sources that you get out of the box are mainly SqlDataSource and ObjectDataSource. Of course, you can also build your own data source. The nice thing is that with ObjectDataSource, you can potentially get data from any data store.
Now, for hierarchical data sources, you've got XmlDataSource and you've got SiteMapDataSource, and... and that's it. So if you want to populate a Menu or TreeView from a database, you'll pretty much have to do it from code.
Enter CompositeHierarchicalDataSource... This control composes several tabular data sources into one hierarchical data source using relations.
Here's a simple page that displays a menu that takes its data from a composition of one ObjectDataSource and two AccessDataSources:

<%@ Page Language="C#" debug="true" %>
<%@ Register Namespace=MyControls.DataSources.CompositeHierarchicalDataSource TagPrefix=my %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >

<head id="Head1" runat="server">
   <title>CompositeHierarchicalDataSource</title>
</head>

<body>
<form id="form1" runat="server">
   <div>
      <my:CompositeHierarchicalDataSource runat=server ID=Composite1 RootViewName="Categories:DefaultView">
         <DataSources>
            <asp:ObjectDataSource ID="Categories" Runat="server" TypeName="Categories"
               SelectMethod="GetCategories" />
            <asp:AccessDataSource ID="SubCategories" Runat="server" DataFile="~/data/things.mdb"
               SelectCommand="SELECT [Id], [CategoryId], [Name] FROM [SubCategories]"/>
            <asp:AccessDataSource ID="Things" Runat="server" DataFile="~/data/things.mdb"
               SelectCommand="SELECT [Id], [SubCategoryId], [Name], [Description], [Url] FROM [Things]"/>
         </DataSources>
         <Relations>
            <my:Relation ParentDataSourceId="Categories" ParentView="DefaultView" ParentColumns="value"
               ChildDataSourceId="SubCategories" ChildView="DefaultView" ChildColumns="CategoryId"/>
            <my:Relation ParentDataSourceId="SubCategories" ParentView="DefaultView" ParentColumns="Id"
               ChildDataSourceId="Things" ChildView="DefaultView" ChildColumns="SubCategoryId"/>
         </Relations>
      </my:CompositeHierarchicalDataSource>

      <asp:Menu Runat=Server ID=myMenu DataSourceID=Composite1>
         <DataBindings>
            <asp:MenuItemBinding DataMember="Categories:DefaultView" TextField="text" ValueField="value" />
            <asp:MenuItemBinding DataMember="SubCategories:DefaultView" TextField="Name" ValueField="Id" />
            <asp:MenuItemBinding DataMember="Things:DefaultView" TextField="Name" ValueField="Id"
               ToolTipField="Description" NavigateUrlField="Url" />
         </DataBindings>
      </asp:Menu>
   </div>
</form>
</body>
</html>
 

You can in principle use any tabular data source to build your hierarchical data source. In principle only: this is still a proof of concept and is currently limited to SqlDataSources, ObjectDataSources whose views return DataViews, and datasources whose views implement ITabularDataSourceView. Another problem it currently has is that it loads all of the data at one time. It would be nice to have lazy loading for populate on demand TreeView scenarios.

Last edited Jul 4, 2007 at 12:47 AM by BertrandLeRoy, version 2