Processing of hierarchical data in ASP.net 2.0

Source: Internet
Author: User
Tags filter bind eval expression xpath
asp.net| data

Data source controls can expose flat-table or hierarchical data at the same time. The SqlDataSource and ObjectDataSource controls shown earlier are all tabular data source controls. ASP.net 2.0 also contains two hierarchical data source controls: XmlDataSource for connecting XML files and SiteMapDataSource for connecting to site navigation data. This section describes the techniques used for these controls.

TreeView and Menu Controls

A data-bound control is similar to a data source control, or it can be hierarchical. Tabular data-bound controls display data lists or tables, hierarchical data-bound controls can get hierarchical data in a recursive way, and the parent-child relationship is used to display data in the UI. Examples of hierarchical data-bound controls in ASP.net 2.0 are the TreeView and Menu controls. The following are some techniques for binding these controls to a hierarchical data source, including many examples.

Binding to XML

The XmlDataSource control allows other controls to bind to XML data. The XmlDataSource supports the DataFile property, which is used to specify the path to the XML data file as input (input). You can also specify the Tranformfile attribute, apply an XSLT transformation to the data, and set the XPath property to specify a subset of the data source nodes that need to be exposed.

The following example shows a TreeView control that is bound to an XML file through a XmlDataSource control. This TreeView associates the properties of each TreeNode object with the attributes of the XML node in the hierarchy tree (for data binding, the attributes of the XML node are processed as properties of the data item). By default, the TreeView control simply displays data items by calling the object's ToString () method. It shows the element name of the XML node so that you can see the node hierarchy that the TreeView binds to. It doesn't necessarily show what you need, but it provides a good starting point and you'll be more likely to customize the way XML data is displayed in the future.


Expanddepth= "3" maxdatabinddepth= "3" runat= "Server"/>

In order for the TreeView to display more meaningful content, you can specify different data bindings for each node in the tree. To define how the fields of a hierarchical data item are mapped to the TreeNode property, you can add the TreeNodeBinding object to the DataBindings collection of the TreeView. The two important properties of treenodebinding determine how to use bindings on a collection of hierarchical data items. The DataMember property specifies the type of the data item or the name of the element used for binding in the XML data. The Depth property specifies the depth of the data binding applied to the hierarchy tree. You can set datamember or depth, or two properties. For example, if you want to define data binding for all book elements in an XML file, simply set the DataMember to "book". To define data binding for all nodes with a depth of 1, simply set the depth to 1. If you want to define all the book nodes with a depth of 1, you need to set the DataMember of the TreeNodeBinding object to "book" and set the depth to 1.

When you set the DataMember or depth to match the node set, You can define additional properties of treenodedatabinding to customize how the properties of the data item (or XML node properties in the XML data) map to the TreeNode properties of the TreeView control. For example, the TextField property defines the name of the property that is displayed as TreeNode text, and similarly, the Valuefield property defines the data item property as the TreeNode value, and the Navigateurlfield property defines the field for the TreeNode Navigation link. Properties, and so on. You can also specify a static value for a TreeNode property that already has a data binding. For example, the TreeNode of the book element is specified using the "book.gif" image, setting the ImageUrl property of the treenodebinding of the DataMember property that is "book".

The following example shows a TreeView bound to XML data that is applied only to specific elements of the XML hierarchy tree.




XmlDataSource supports XPath properties, which you can use to filter the set of nodes exposed by the data source. In the following example, the XPath property is set to bookstore/genre[@name = ' Business ']/book to filter the node of the data source, showing only the book element under the "Business" type. You should be careful when specifying the syntax of an XPath property, or you may experience a situation where no node of the data source is exposed (the associated data-bound control will not appear).

Note that the TreeView tree accurately matches the hierarchy in the source XML. For this reason, XML is typically constructed to bind to the TreeView, or the XSL transformation is used to reconstruct the data to the appropriate hierarchical structure in order to bind to the TreeView.

It is also possible to bind a tabular data-bound control to a hierarchical data source, but it can only display the first tier of data. In the following example, the templated DataList control is bound to a bookstore XML file. Because the data source exposes a top-level node that is a node, DataList can bind to properties of these nodes in its own ItemTemplate template using the Eval data binding syntax.


"
<%# Eval ("Title")%>
ISBN: <%# Eval ("ISBN")%>
Price: <%# Eval (' price ')%>


Although it is useful to show only one layer, it should be better if you can use nested tabular data-bound controls to display the following levels. Luckily, ASP.net 2.0 allows you to implement this functionality. In addition to the Eval data binding syntax, ASP.net 2.0 provides an XPath based data binding syntax that can be used on any data item that implements the IXPathNavigable interface. There are two types of expression available:

· XPath (expression, [formatstring])-computes the value of an XPath expression based on the data item, returning a single value.

· XPathSelect (expression, [formatstring])-computes the value of the XPath expression based on the data item and returns the list of nodes.

The following example is based on the previous example, which uses an XPath data-binding expression instead of an eval expression to bind to the properties of the book node. On the surface, such an operation does not have much more functionality than the "@" prefix for each expression, just the XPath syntax used to refer to the node's properties. However, the real flexibility of XPath is the ability to rely on any item in this reference hierarchy (not just attributes).

This example adds an additional DataList to the external DataList ItemTemplate template and binds the internal DataList property to a chapter expression that describes the XPathSelect child node of the current book node. In the internal DataList ItemTemplate template, XPath data-binding expressions compute values based on these "chapter" content nodes. ASP.net 2.0 utilizes this technique to enable you to easily construct rich, layered data display methods by combining tabular data-bound controls.


"
<%# XPath ("@Title")%>
ISBN: <%# XPath ("@ISBN")%>
Price: <%# XPath ("@Price")%>


To handle requests from a data-bound control to a node at a particular location, a hierarchical data source control such as XmlDataSource makes each node in the hierarchy correspond to a unique path. This brings some features, such as the TreeView's demand-filled (popoulateondemand) feature, when a node is extended and the node from the data source is sent to the client instead of sending all the nodes one at a time. It also allows you to configure the data source in this way in the page code to display nodes in a particular location. The path syntax for different data types is different and cannot be constructed in code. However, you can use the TreeNode DataPath property to access the data path of the node that is bound to the TreeView. Because XmlDataSource use XPath expressions as their own data path syntax, these paths can also be assigned to XmlDataSource XPath properties to filter the node list. The following example illustrates this technique, which uses XmlDataSource to implement a master-from table. The example has two XmlDataSource controls, one bound to the TreeView (the primary Table control) and the other bound to the DataList (from the Table control). When the user clicks on the TreeView node, it retrieves its datapath attribute and assigns it to the XmlDataSource control that is bound to DataList to display the details of the clicked node.

Sub mytreeview_selectednodechanged (sender as Object, E as EventArgs)
Dim path as String = MyTreeView.SelectedNode.DataPath
Mydetailssource.xpath = Path
Mydatalist.datasource = Mydetailssource
Mydatalist.databind ()
End Sub
Bind to site navigation data (site navigation)

Site navigation data is another level of data in asp.net. asp.net 2.0 not only supports programmatic access to site map data using the site navigation API in ASP.net, but also supports declarative data binding using SiteMapDataSource controls. When you bind the TreeView (or menu) control to SiteMapDataSource, the text and URL properties of the site map can be bound to TreeNode (or MenuItem). Although you can use a collection of data bindings to establish this binding, such an operation is not necessary. The TreeView and Menu controls automatically bind the text and NavigateUrl properties of the TreeNode or MenuItem to the associated site map attributes (which are implemented using the SiteMapNode Inavigateuidata interface). When the TreeView and menu are bound to SiteMapDataSource, they also have an attribute that automatically sets the SelectedNode or SelectedItem property to the current node in the site map.

The following example shows a TreeView bound to a SiteMapDataSource control. Although this example uses a collection of data bindings, this is not necessary if you only need to bind to the node's text and URL properties.


Datasourceid= "Sitemapsource" runat= "Server"



Binding to a relational database

When multiple tables in a relational database are associated with a foreign key, they can also be expressed in a hierarchical structure. For example, in a product database, a product is associated with a product category, and they can be represented by a hierarchy between categories and products (1 to many). Although the current ASP.net version does not contain a control that displays relational data as a hierarchy, you can still accomplish this by programmatically populating the node/data items of a hierarchical data-bound control such as TreeView or menu. The following example shows a TreeView control populated with a relational database. This example uses the PopulateOnDemand feature of the TreeView to populate child nodes on demand (when a TreeNode is extended on the client).

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
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








Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.