Displaying Hierarchical Data in a GridView Control
So far, you've seen how to display hierarchical information in a TreeView control and a FormView control. This section will provide you with an example on how to bind the XmlDataSource control to a DropDownList control and a GridView control. The example code uses the same ProductDetails.xml
file as the XML source that you've already seen. As in the previous examples, add an XmlDataSource control to the ASP.NET page, but set the XPath attribute of the XmlDataSource control to Products/Product
. This will filter for all the Product
elements contained inside the root Products element.
Now, add a DropDownList and a GridView control to the page and set the DataSourceID
attributes for both to the name of your XmlDataSource control (XmlDataSource1
by default). In the SelectedIndexChanged
event of the XmlDataSource control, add code to set the XPath property of the XmlDataSource control to the value selected by the user in the DropDownList control. Finally, add a button to the ASP.NET page and inside the Click event of the button, add code to reset the XPath property of the XmlDataSource control to its original value (which is Products/Product). After making all of the changes, the code should look similar to Listing 1
The code shown in Listing 1
uses the SelectedIndexChanged
event of the DropDownList control to dynamically set the XPath property of the XmlDataSource control to the expression:
XmlDataSource1.XPath = "Products/Product[@ID=" + _
DropDownList1.SelectedValue + "]"
In other words the XPath expression uses the value selected by the user in the DropDownList to obtain the Product element with that ID attribute value. In the button's Click
event, the code resets the XPath property to the default value, which is also set in the XPath attribute as part of the XmlDataSource control declaration.
Executing the code in Listing 1
results in a page where, when users select a product in the DropDownList, the output displayed by the GridView varies accordingly (see Figure 4
Caching an XmlDataSource Control
|Figure 4. A Bound GridView Control: When a user selects an item in the DropDownList, the page displays associated items in the GridView based on the ID attribute of the item selected.|
For performance purposes, caching is enabled for the XmlDataSource control by default. You typically use the XmlDataSource with an XML fileand opening and reading an XML file on the server every time a page is requested can have a negative impact on the performance of your application. Caching reduces the processing load on your servers at the expense of memory; but in most cases this is a good trade-off. To cache data in an XmlDataSource control, set the EnableCaching
property to true
. You also need to set the CacheDuration
property to the number of seconds that the cache stores data before refreshing the cache. You can also use the CacheExpirationPolicy
to gain a finer level of control over the caching behavior.
This article showed you how to take advantage of the XmlDataSource control in an ASP.NET page to connect to XML data sources and display that information in data-bound controls such as a TreeView, FormView, GridView or DropDownList. Using the XmlDataSource control, you can apply an external XSL file to transform the contents of the XML file into another format before displaying that information in a data-bound control, and you can use XPath queries to extract the data.