Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

XML Data-Binding Powers Up in ASP.NET 2.0 : Page 2

In ASP.NET 2.0, the new XmlDataSource control greatly simplifies binding XML sources to controls, letting you display hierarchical XML data easily. It even leverages XSLT, XPath, and caching, eliminating much manual coding.


advertisement
XPath Data Binding Syntax
By default, the XmlDataSource control loads all the XML data in the XML file identified by the DataFile property or found inline in the Data property, but you can filter the data using an XPath query. The XPath property supports an XPath-syntax filter that the XmlDataSource control applies after it loads and transforms the XML data. The control supports the XPath-based data binding syntax on any data item that implements the IXPathNavigable interface. The syntax of the XPath expression is:

XPath(expression, [formatString])

The property evaluates an XPath expression against the data item and returns a single value.

The following code example uses XPath data binding expressions to bind to attributes of the Product nodes. This particular example simply uses the '@' prefix—the XPath syntax for a node attribute—to retrieve the attribute value in each expression. However, you can also use the XPath expression to refer to arbitrary items within the hierarchy.

<%@Page Language="VB" Debug="True" %> <html> <head> <title>XPath Select Method Example</title> </head> <body> <div class="heading"> XPath Select with a FormView Control</div> <hr/> <form id="Form1" runat="server"> <asp:FormView ID="Formview1" runat="server" DataSourceID="XmlDataSource1" AllowPaging="True" BorderWidth="1px" BorderColor="#CC9966" BorderStyle="None" BackColor="Blue"> <RowStyle ForeColor="#3333ff" BackColor="#cccccc"></RowStyle> <PagerStyle ForeColor="#cc3300" HorizontalAlign="Center" BackColor="#FFFFCC"></PagerStyle> <ItemTemplate> ID:<b> <asp:Label ID="lblID" Runat="server" Text='<%# XPath("@ID") %>' /></b><br/> Name:<b> <asp:Label ID="lblName" Runat="server" Text='<%# XPath("@Name") %>' /></b><br /> Price:<b> <asp:Label ID="lblPrice" Runat="server" Text='<%# XPath("@Price") %>' /></b><br /> </ItemTemplate> </asp:FormView> <asp:XmlDataSource id="XmlDataSource1" runat="server" DataFile="~/Data/ProductDetails.xml" /> </form> </body> </html>

 
Figure 2. The XPath Example ASP.NET Page. The figure shows the result of data-binding a FormView control to the XmlDataSource control.
The ASP page described in the preceding code binds a FormView control to the XmlDataSource control. FormView is a new control supplied with ASP.NET 2.0, which can be directly associated with a DataSource control. After making the initial association, the FormView control, in conjunction with the DataSource control, handles Insert, Delete, Update, and Get operations. The example creates an ItemTemplate element inside the FormView control and specifies the XPath expression as part of the Label declarations. Figure 2 shows how the results look in a browser.

Paging is enabled in the screen shown in Figure 2, and users can click on the page numbers to navigate to the details of different products.



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date