Programming Microsoft ASP.NET 4 - Dino Esposito [230]
The DataBinder Class
Earlier in this chapter, you met <%# … %> expressions in the context of templates, along with the Eval method. The Eval method is a kind of tailor-made operator you use in data-binding expressions to access a public property on the bound data item. The Eval method we used in past code snippets is a shortcut method defined on the Page class that wraps the services of another Eval method, but one that’s defined on another class—DataBinder.
Important
Through the Eval method—even if it comes from DataBinder or Page—you can access public properties on the bound data item. A data-bound control is linked to a collection of data objects. The data item just represents the element in the bound data source that is being processed at some point. Therefore, the Eval method ends up querying the data item object for its set of properties.
The DataBinder class supports generating and parsing data-binding expressions. Of particular importance is its overloaded static method Eval. The method uses reflection to parse and evaluate an expression against a run-time object. Clients of the Eval method include Rapid Application Development (RAD) tools such as Microsoft Visual Studio .NET designers and Web controls that declaratively place calls to the method to feed dynamically changing values to properties.
The Eval Method
The syntax of DataBinder.Eval typically looks like this:
<%# DataBinder.Eval(Container.DataItem, expression) %>
A third, optional, parameter is omitted in the preceding snippet. This parameter is a string that contains formatting options for the bound value. The Container.DataItem expression references the object on which the expression is evaluated. The expression is typically a string with the name of the field to access on the data item object. It can be an expression that includes indexes and property names. The DataItem property represents the object within the current container context. Typically, a container is the current instance of the item object—for example, a DataGridItem object—that is about to be rendered.
The code shown earlier is commonly repeated, always in the same form. Only the expression and the format string change from page to page.
A More Compact Eval
The original syntax of the DataBinder.Eval can be simplified in ASP.NET by writing the following:
<%# Eval(expression) %>
Any piece of code that appears within the <%# … %> delimiters enjoys special treatment from the ASP.NET runtime. Let’s briefly look at what happens with this code. When the page is compiled for use, the Eval call is inserted in the source code of the page as a standalone call. The following code gives you an idea of what happens:
object o = Eval("lastname");
string result = Convert.ToString(o);
The result of the call is converted to a string and is assigned to a data-bound literal control—an instance of the DataBoundLiteralControl class. Then the data-bound literal is inserted in the page’s control tree.
The TemplateControl class—the parent of Page—is actually enriched with a new protected (but not virtual) method named Eval. The following pseudocode illustrates how the method works:
protected object Eval(string expression)
{
if (Page == null)
throw new InvalidOperationException(...);
return DataBinder.Eval(Page.GetDataItem(), expression);
}
As you can see, Eval is a simple wrapper built around the DataBinder.Eval method. The DataBinder.Eval method is invoked using the current container’s data item. Quite obviously, the current container’s data is null outside a data-binding operation—that is, in the stack of calls following a call to DataBind. This fact brings up a key difference between Eval and DataBinder.Eval.
Getting the Default Data Item
The pseudocode that illustrates the behavior of the page’s Eval method shows a GetDataItem method from the Page class. What