Creating an Object for a List from a Collection Element
The following steps describe how a mobile List control constructs a MobileListItem from an element in the collection:
- The control checks whether the DataTextField or DataValueField properties are defined. If they are, the control uses these field names to discover properties from the element and set the Text and Value properties of the MobileListItem.
- If neither the DataTextField nor DataValueField properties are defined, the control sets the Text and Value properties of the MobileListItem to the string representation of the element (by using the ToString method).
- If an ItemDataBind event handler is defined, it calls the handler. You can use this handler to set properties of the MobileListItem from arbitrary expressions.
When providing default rendering, the list control represents a MobileListItem by its Text property. In templated rendering, the template can render a desired property of the MobileListItem or associated data-bound object.
When the ItemsAsLinks property is set, the List control renders items as hyperlinks. The value of the Text property becomes the link text, and the value of the Value property becomes the target URL.
Handling the Selection
If your list is complex, such as an array of objects, you cannot directly access the members of the selected item through the selection. However, if you design your application appropriately, you can access the associated object. If you are creating a group of objects to populate your list, you can make the group a global array, and then handle the returned value as an index into the array, as shown in the following code.
[C#]
Person[] customers = new Person[3] {
new Person("George Washington", "George", "GW"),
new Person("Abraham Lincoln", "Abe", "AL"),
new Person("Theodore Roosevelt", "Teddy", "TR")
};
class Person
{
private String _Name, Nickname,_Initials;
public Person(String name, String nickname, String initials)
{
this._Name = name;
this._Nickname = nickname;
this._Initials = initials;
}
public String Name { get { return _Name; } }
public String Nickname { get { return _Nickname; } }
public String Initials { get { return _Initials; } }
}
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
// Bind the array to the list.
List1.DataSource = customers;
List1.DataTextField = "Name";
List1.DataBind();
}
}
private void List1_ItemCommand(object sender, System.Web.UI.MobileControls.ListCommandEventArgs e)
{
Person selectedPerson = customers[e.ListItem.Index];
Label1.Text = selectedPerson.Name
+ " (aka " + selectedPerson.Nickname + "),"
+ " initials " + selectedPerson.Initials;
ActiveForm = Form2;
}