Team LiB
Previous Section Next Section

Data Binding Mechanics

The next section will dig a little deeper and cover some of the nuts and bolts of the data binding system for ASP.NET, including some of the most commonly called methods and properties, such as Container.DataItem, DataBinder.Eval, and the ItemDataBound event.

Container.DataItem

Container.DataItem is a property on a class that is made available to ASP.NET controls at runtime. The Container class instance's DataItem property indicates the item to which the current control's container has been bound. This property is used within templates to provide additional user interface functionality. Templates are formatting models that are instantiated for each row bound. When creating an item template, you can use Container.DataItem to refer to the piece of data to which the current item has been bound. You will see more of the ItemTemplate and other templates when you see some sample binding with Repeaters, DataLists, and DataGrids.

DataBinder.Eval

The DataBinder class is a noninheritable class that is used for one purpose: data binding shortcuts. It has a single method, Eval, that is used to evaluate data binding expressions at runtime and return type-safe results.

The reason for using this syntax instead of using the standard data binding expression syntax is because the Eval method uses Reflection to map the binding expression to a data-bound object. It performs type casting and type conversion so that the developer does not have to worry about it at design time. This method is what makes it possible for Visual Studio .NET 2003 to enable the developer to write data binding expressions in a design tool against an object that will not be instantiated until runtime.

The Eval method can take up to three arguments. The first two arguments are required. The first parameter is the data source object on which the evaluation should be performed. This is very often a dynamically bound object supplied by ASP.NET that you can refer to with the Container.DataItem syntax. The second parameter is the name of the member on the data source to evaluate. The third (and optional) parameter is a format string that dictates the format into which to convert the output.

For example:

<%# DataBinder.Eval( Container.DataItem, "RetailPrice", "{0:c}" %>

This outputs the value of the RetailPrice member on whatever object is currently bound in the localized currency format.

The only caveat here is that this method uses Reflection. If you know at design time everything you need to know to perform your binding, you can get away without using the DataBinder.Eval method and bypass the performance penalty you would incur by using Reflection.

The ItemDataBound Event

When you think of binding, you might think of it taking place in one big event. For example, you bind a DataGrid to a table that has 400 rows in it. To the naked eye, it looks like the grid was simply bound to the entire set of data and that's all that took place.

In reality, each row is individually bound to the data, and rendered using a template that dictates how that row is to be displayed and rendered to the client browser. When a row (or any element in an IEnumerable list) is taken from the data source and bound to the various templates, an event is triggered. This event is OnItemDataBound, and every bindable ASP.NET control implements it.

Everyone who has ever tried to use data binding knows that binding on its own is almost never enough. You might need custom formatting in some conditions; you might want to insert line breaks or an <hr> tag at certain points; you might want to enclose a specific record in a border if it is the currently selected record, and so on. Whatever the reason, if you want control over the process that takes place when an element is bound to a control, you can take advantage of the OnItemDataBound event. Each control that implements OnItemDataBound supplies its own special type of arguments to the event. Table 31.1 shows the class that implements the event and the name of the event argument class that is supplied with it. You can look the event argument class up in MSDN for a detailed list of all the properties.

Table 31.1. Control Classes and the Event Arguments Passed for Item Data Binding

Control Class

Event Argument Class Passed

Repeater

RepeaterItemEventArgs

DataGrid

DataGridItemEventArgs

DataList

DataListItemEventArgs


    Team LiB
    Previous Section Next Section