Share via


Data Binding and Viewing Data Using an ObjectList Control

You can use the ObjectList ASP.NET mobile control to provide a more versatile view of data. This control shows two views of your data source, a list view that shows a summary of each item, and a view that shows item details. You can explicitly define the list fields that you want to show for each item, or you can automatically generate them from the data source. By default, the control generates a field for each field in the data source, in the order it appears within the data. The name of each automatically generated field is used as the field title.

You can bind data in an ObjectList to a DataView or a DataSet. To bind data in an ObjectList mobile control to a DataView, set the DataSource property, and then call the DataBind method to perform data binding. For example, if you have a DataSet with a table named Titles, you can use the following statements to perform data binding.

myObjectList.DataSource = ds.Tables["Titles"].DefaultView;
myDataList.DataBind();

Alternatively, to bind data directly to a DataSet, you must additionally set the DataMember property to the name of the table. The following example is equivalent to the previous one.

myObjectList.DataSource = ds;
myObjectList.DataMember = "Titles";
myObjectList.DataBind();

You can also set an item field to a value composed of several of the data item's properties. To do this, you can override the ItemDataBind event of the control, and set the field programmatically. The following example sets the Summary field to a combination of the title and price of a book.

private void ObjectList_OnItemDataBind(Object sender, 
  ObjectListDataBindEventArgs e)
{
   e.ListItem["Summary"] = String.Format( String.Format ("{0} – ${1}", 
      DataBinder.Eval(e.DataItem, "title"),
      DataBinder.Eval (e.DataItem, "price")));
}

In addition, you can control how each item is rendered in the list view. By default, the list view uses the first field to represent each item. However, you can set the LabelField property to any defined or automatically generated field, including one that is not visible in the details view. Using the previous example, you can use the Summary field as the label for an item, while hiding it in the details view.

Performing Data Binding Within an ObjectList

An ObjectList control shows content only if it is bound to a data source. The data source can be any type that implements the IEnumerable interface. However, each object in the data source must be of the same class, or must inherit from the same common class. When the AutoGenerateFields is set to true, the objects in the data source must be of the same class.

For each object in the data source, the control constructs an ObjectListItem and stores it in its Items member collection. This collection can be inspected, but not modified, by the application.

All properties that the object list refers to must be public properties of a class common to all objects in the data source. All properties that fields refer to must also be of a bindable type. Valid bindable types are String, DateTime, Currency, Decimal, and the set of primitive types.

For each object in the data source, the control performs the following data binding steps:

  1. For each field, the control uses the field's DataField property to determine which property of the data object to look up. Each value is saved in the ObjectListItem as an indexed field value.

  2. After all fields are bound in this way, the control calls any ItemDataBind event handler that is defined for the control. You can use this handler to do more complex data binding and to set values in the ObjectListItem.

    Note   Some changes to an ObjectList require you to rebind the data on that control. These changes include adding or removing fields, changing the DataField property of a field, and changing the DataFormatString property of a field.

Initiating Automatic Field Generation During Data Binding

At data binding, if the AutoGenerateFields property is set to true, the control inspects the data source and then automatically generates fields. If the data source is a typed list of type ITypedList, the control inspects the information for the type. Otherwise, the control inspects the type information of the first object in the list.

For each bindable public property of the inspected type, the control generates a field bound to the property. However, fields are bound during data binding. If you make changes to a field, or add or remove fields, you must bind the properties again.

By default, automatically generated fields are visible, use default formatting, and have the same title as the property name. All these can be changed programmatically. Also, you can specify the title of the field by adding an ObjectListTitleAttribute attribute to the property. For example, if the object has a property declared as [ObjectListTitle("Address")]myAddress, the generated field will have the title "Address."

If the ObjectList control has explicitly defined fields, auto-generated fields are added after them.

Associating Commands Within an Object List

The ObjectList control allows a set of commands to be associated with an item. Each command has a Name property used to uniquely identify the command, and a Text property used to render the command.

The control provides two ways to define commands:

  • Declaratively, by using <Command> child elements within an ObjectList control.
  • Programmatically, by constructing ObjectListCommand objects and adding them to the Commands member collection of the control.

By default, all items in the list share the same set of commands. However, before rendering the set of commands for a given item, the control raises the OnShowItemCommands event. An event handler can use this to modify the set of commands specifically for the item.

When the user selects a command, an ItemCommand event is raised, with information about the item selected and the name of the command selected.

Even if you define a default command for an item, you must include a command by the same name in the Commands member collection. If the control cannot render a UI that includes a shortcut for the default command, it must display the default command as part of the set of commands.

Accessing Field Values of a List Item

When you associate an event handler with an ObjectList control, it renders list items as interactive elements; clicking an item in a list generates an event that retrieves the appropriate action for that item. During data binding, each field is bound to its corresponding property.

To retrieve a field value from an ObjectListItem object, use the following syntax, where lstItem is an ObjectListItem.

lstItem[fieldName]

For example, if you are accessing an employee database, you might have code similar to the following statements.

ObjectList1.DataSource = ds.Tables["Employees"].DefaultView;
ObjectList1.DataMember = "Employees";
ObjectList1.LabelField = "LastName";

Data Binding in ObjectList Templates

In the ObjectList control, you can define various templates to customize the user's experience. If you are doing inline data binding in these templates, use the following syntax:

<%#((ObjectListItem)Container)["BookName"]%> 

You can also use the DataBinder.Eval method to bind data in all templates, shown as follows:

<%#DataBinder.Eval(((ObjectListItem)Container).DataItem,"fieldname")%>

See Also

Accessing Data Using Listing Controls