The New and Simplified Data Binding Syntax

You may have noticed that the code we used in the examples earlier in this chapter takes advantage of a simplified syntax for the data binding expressions. ASP.NET 2.0 fully supports the previous (version 1.0) syntax but adds a couple of new features as well. There is the simplified syntax for binding to nonhierarchical (rows and columns ) data, plus new techniques that allow binding to hierarchical data such as XML documents.

The ASP.NET 1.0 Syntax for Data Binding

In ASP.NET 1.0, there are two ways to bind to data in a control that supports data binding. One choice is to use the early-bound approach that references the container directly:

 <%# Container.DataItem("  expression  ") %> 

The expression is usually a column name from the rowset, though it can be the name of a property or field exposed by the object bound to the control.

The alternative, which is useful if you want to format the value, is the late-bound approach using the Eval method of the DataBinder responsible for carrying out the data binding:

 <%# DataBinder.Eval(Container.DataItem,              "  expression  "[, "  format  "]) %> 

In this case, the optional format string can be used to output markup and literal content, with the value inserted into the string at the point where a {0} placeholder is located. The placeholder can also include a standard format string character to format the value at the same time, for example:

 <%# DataBinder.Eval(Container.DataItem, _              "TotalPrice", "Total Price: {0:C}") %> 

You can see one of the issues that come into play here. The statement is verbose, and yet the majority is exactly the same in every occurrence. Moreover, because the content is executable code (the Eval method), line continuation characters are required in Visual Basic. Yet this is the only way to exert any real control over the format and content of the output.

Simplified ASP.NET 2.0 Syntax for Nonhierarchical Data Binding

In version 2.0, the most obvious simplification is that DataBinder is now the default context for data binding expressions, so the Eval method can be used like this:

 <%# Eval("  expression  "[, "  format  "]) %> 

The preceding example then becomes just:

 <%# Eval("TotalPrice", "Total Price: {0:C}") %> 

And, if no formatting of the value is required, you can use:

 <%# Eval("TotalPrice") %> 

Simplified ASP.NET 2.0 Syntax for Hierarchical (XML) Data Binding

ASP.NET 2.0 introduces new data source controls that can expose hierarchical data from XML documents. When a control is bound to such a data source control, the data is exposed to the control as a hierarchical structure that cannot be bound using the existing data binding syntax.

Instead, a development on the existing techniques is used, based on a new XPathBinder object exposed by the list control when bound to XML data. The XPathBinder exposes two methods named Eval and Select . Both take an instance of an object that exposes the IXPathNavigable interface (such as the container for a data source control bound to an XML document). The second parameter is an XPath expression that selects one or more nodes from the source data.

The Eval (XPath) Method

The Eval method returns a single value from the current "row" (a single element value) and can optionally format it using the same approach as described for nonhierarchical data in the previous section:

 <%# XPathBinder.Eval(Container.DataItem,               "  expression  "[, "  format  "]) %> 

However, the simplified syntax is the obvious choice here, using an override of the Eval method named XPath :

 <%# XPath("  expression  "[, "  format  "]) %> 

The expression is an XPath that returns a single node (element or attribute) from the fragment or XML document to which the control is bound. The format is the same you use with the Eval method for nonhierarchical (rowset) data.

The Select (XPathSelect) Method

With hierarchical data, each node can be a collection of other nodes. For example, in an XML document, each <employee> element in a list of employees is likely to be the parent of several other nodes (see Listing 4.29).

Listing 4.29 A Sample XML File for Data Binding
 <employee-list>   <employee id="1">     <name>Mike</name>     <department>Sales</department>     <phone>3867</phone>   </employee>   <employee id="2">     <name>Nikita</name>     <department>Marketing</department>     <phone>1442</phone>   </employee>   <employee id="3">     ... etc ...   </employee> <employee-list> 

In this case, a control bound to the data can use the XPath approach to select a specific employee name:

 <%# XPath("employee[@id='2']/name") %> 

However, a useful feature would be to bind a nested list control to each employee element so that it can display the details of each employee. In this case, the data binding statement must return a collection of nodes. The Select method of the XPathBinder does just this:

 <%# XPathBinder.Select(Container.DataItem,               "  expression  ") %> 

Again, there is a simplified approach that uses an override of the Select method called XPathSelect. (There is no format parameter because the method returns a collection and not a single value.)

 <%# XPathSelect("  expression  ") %> 

As an example, the following statement will return all the child elements of the <employee> element that has the id value of 2 :

 <%# XPathSelect("employee[@id='2']") %> 

Simplified Data Binding Syntax Options

In summary, you have five obvious choices for data binding expressions:

A: <%# Container.DataItem("[ column property field ]") %>

B: <%# DataBinder.Eval("[ column property field ]"[, " format "]) %>

C: <%# Eval("[ column property field ]"[, " format "]) %>

D: <%# XPath(" xpath-expression "[, " format "]) %>

E: <%# XPathSelect(" xpath- expression ") %>

Table 4.15 shows how and where they can be used.



A First Look at ASP. NET v. 2.0 2003
A First Look at ASP. NET v. 2.0 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 90

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net