Data Binding a List or SelectionList Control

The List and SelectionList ASP.NET mobile controls can render a basic view of data and provide basic interaction with data items.

A List or SelectionList mobile control can be data-bound to a DataView or a DataSet. To bind data in a List or SelectionList mobile control to a DataView, you can set the DataSource property and 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.

myList.DataSource = ds.Tables["Titles"].DefaultView;
myList.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.

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

A list item in a List or SelectionList control can normally bind to two properties from each data item. The first property becomes the Text property of the list item, and the second becomes the Value property of the list item. These values are set through the DataTextField (DataTextField) and DataValueField (DataValueField) properties of the List (SelectionList) control. The List control renders each item by using its Text property. For example, if you want to render each item by its CustomerName property, set the DataTextField property to CustomerName.

Sometimes, you might want to render each item as a summary composed of several of its properties. To do this, you can override the ItemDataBind event of the List control or the ItemDataBind event of the SelectionList control, and set the Text property. The following example is used to render a book as a combination of title and price.

private void List_OnItemDataBind(Object sender, 
  ListDataBindEventArgs e)
{
    e.ListItem.Text = String.Format ("{0} – ${1}", 
        DataBinder.Eval (e.DataItem, "title"),
        DataBinder.Eval (e.DataItem, "price"));
}

On devices that support richer rendering, you can use a template set for your List control to show a customized view of a data item. In templated mode, the List control functions similarly to the Repeater ASP.NET server control. For example, you can use the following item template to show a detailed view of a book.

<ItemTemplate>
   <tr style="background-color:FFECD8">
      <td>
         <%# DataBinder.Eval(((MobileListItem)Container).DataItem, "title") %>
      </td>
      <td>
         <%# DataBinder.Eval(((MobileListItem)Container).DataItem, "title_id") %>
      </td>
      <td>
         <%# DataBinder.Eval(((MobileListItem)Container).DataItem, "type") %>
      </td>
      <td>
         <%# DataBinder.Eval(((MobileListItem)Container).DataItem, "pub_id") %>
   </td>
   <td>
         <%# DataBinder.Eval(((MobileListItem)Container).DataItem, "price", "$ {0}") %>
      </td>
   </tr>
</ ItemTemplate >

For more information about template sets, see the Template Sets and Templated Controls documentation.

Data Binding in List or SelectionList Templates

You can define a template in the List or SelectionList control to customize the control. To use inline data binding in these templates, use either of the following examples:

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

Or

<%#((MobileListItem)Container).Text%>
<%#((MobileListItem)Container).Value%>

See Also

Accessing Data Using Listing Controls | Differences Between the SelectionList and List Classes