Processing of hierarchical data in ASP.net 2.0

Source: Internet
Author: User
Tags bind eval xpath xsl

ASP.net 2.0 contains two hierarchical data source controls: The XmlDataSource used to connect XML files and the SiteMapDataSource used to connect site navigation data. This section describes the techniques used for these controls.

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.

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.

<asp:XmlDataSource ID="MySource" DataFile="~/App_Data/Bookstore.xml" runat="server"/>
<asp:TreeView ID="TreeView1" SkinId="Bookstore" DataSourceId="MySource"
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.

<Databindings>
 <asp:TreeNodeBinding DataMember="Bookstore" Text="Bookstore"
ImageUrl="~/images/xp/folder.gif" />
 <asp:TreeNodeBinding DataMember="genre" TextField="name"
ImageUrl="~/images/xp/folder.gif" />
</Databindings>

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

<asp:XmlDataSource ID="MySource" DataFile="~/App_Data/Bookstore.xml"
XPath="Bookstore/genre[@name='Business']/book" runat="server"/>

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.

<asp:XmlDataSource ID="MySource" DataFile="~/App_Data/Bookstore2.xml"
TransformFile="~/App_Data/Bookstore2.xsl"
XPath="Bookstore/genre[@name='Business']/book" runat="server"/>

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.

<asp:DataList id="MyDataList" DataSourceId="MySource" runat="server">
 <ItemTemplate>
<img alt="Cover Image" src='<%#"images/" + Eval("ISBN") + ".gif"%>'>
<%# Eval("Title") %>
ISBN: <%# Eval("ISBN") %>
Price: <%# Eval("Price") %>
 </ItemTemplate>
</asp:DataList>

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.