Data Binding a List or SelectionList Control
The List and SelectionList ASP.NET mobile controls render a basic view of data and enable users to select data items.
You can bind a List or SelectionList mobile control to a DataView or a DataSet object, or to any object that implements IEnumerable or IListSource. To bind a List or SelectionList mobile control to a DataView object, you set the control's DataSource property and call its DataBind method. The following code example demonstrates how to bind a control to a DataSet object that contains a table named Titles
.
myList.DataSource = ds.Tables["Titles"].DefaultView;
myList.DataBind();
Alternatively, you can bind a List or SelectionList control to a DataSet object. To do so, you set the DataMember property to the name of the table. The following example is equivalent to the preceding one.
myList.DataSource = ds;
myList.DataMember = "Titles";
myList.DataBind();
A list item in a List or SelectionList control can bind to two data values. One data value is bound to the list item's Text property, and the other is bound to its Value property. You configure binding for list items by setting the DataTextField (System.Web.UI.MobileControls.SelectionList.DataTextField) and DataValueField (System.Web.UI.MobileControls.SelectionList.DataValueField) properties of the List or SelectionList control. The List control displays each item using its Text property. For example, if you want to display each item by its CustomerName property, set the DataTextField property to CustomerName.
You might want to display each item as a summary composed of several data values. To do this, you can handle the ItemDataBind event of the List control or the ItemDataBind event of the SelectionList control, and set the Text property programmatically. The following code example demonstrates how to render book information 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", "{0:C}"));
}
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 like 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}", "{0:C}") %>
</td>
</tr>
</ ItemTemplate >
For more information about template sets, see Template Sets and Templated Controls.
See Also
Concepts
Accessing Data Using Listing Controls
Differences Between the SelectionList and List Classes