TreeNodeBinding TextField="Title" NavigateUrlField="Url" /> </Databindings> </asp:TreeView>
绑定到关系数据库
当关系数据库中的多个表通过外部键相关联的时候,也可以用层次结构来表现。例如,在产品数据库中,产品与产品类别关联,它们就可以用类别和产品之间的层次(1对多)关系来表现。尽管当前的ASP.NET版本没有包含一个用于把关系数据显示为层次结构的控件,你仍然可以通过编程填充层次的数据绑定控件(例如TreeView或Menu)的节点/数据项来实现这种目的。下面的例子显示了一个用关系数据库填充的TreeView控件。这个例子利用TreeView的PopulateOnDemand特性,按需求(客户端上扩展某个TreeNode的时候)来填充子节点。
Sub GetProductCategories(ByVal node As TreeNode) Dim categories As CategoryList = WarehouseDB.GetProductCategories() Dim c As Category For Each c In categories Dim newNode As TreeNode = New TreeNode(c.Name, c.Id) newNode.SelectAction = TreeNodeSelectAction.Expand newNode.PopulateOnDemand = True node.ChildNodes.Add(newNode) Next End Sub Sub GetProductsForCategory(ByVal node As TreeNode) Dim categoryId As String = node.Value Dim products As ProductList = WarehouseDB.GetProductsForCategory(categoryId) Dim p As Product For Each p In products Dim newNode As TreeNode = New TreeNode(p.Name, p.Id) node.ChildNodes.Add(newNode) Next End Sub Sub PopulateNode(ByVal source As Object, ByVal e As TreeNodeEventArgs) Select Case e.Node.Depth Case 0 GetProductCategories(e.Node) Case 1 GetProductsForCategory(e.Node) End Select End Sub <asp:TreeView ID="TreeView1" OnTreeNodePopulate="PopulateNode" SkinId="Simple" Width="250" ExpandDepth="0" runat="server"> <Nodes> <asp:TreeNode Text="Inventory" SelectAction="Expand" PopulateOnDemand="true"/> </Nodes> </asp:TreeView>
|