Differences Between the SelectionList and List Classes

Although both ASP.NET mobile controls classes are very close in design, the fundamental difference lies in the functionality for the developer at design and run time. Both classes maintain a collection of list items. However, whereas the List class is derived from the PagedControl class and ultimately the MobileControl class, the SelectionList control is derived directly from the MobileControl class and does not have pagination handling properties, such as the ItemWeight property.

The major difference between the classes is that the SelectionList class maintains the selection of single or multiple selected items. The SelectType property contains the ListSelectType enumerated value, which determines whether the SelectionList is in single or multiple selection mode. With the List class, you can choose only single items in a list, whereas the SelectionList class provides a rich assortment of enumerated types to choose from: CheckBox, DropDown, ListBox, MultiSelectListBox, and Radio.

SelectionList Functionality

The control is in single selection mode when you set the SelectType property to the following:

  • DropDown
  • ListBox
  • Radio

To retrieve the current selected item while in single selection mode, use the SelectedIndex and Selection properties.

The CheckBox and MulitSelectListBox enumerated values indicate multiselect mode. To retrieve a selected item, query each item's Selected Property.

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, make the group a global array, and handle the returned value as an index into the array, as follows.

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.
        SelectionList1.DataSource     = customers;
        // Display the name to the user.
        SelectionList1.DataTextField  = "Name";
        SelectionList1.DataBind();

        // Set the SelectType.
        SelectionList1.SelectType = 
            System.Web.UI.MobileControls.ListSelectType.CheckBox;
    }

}

private void Command1_Click(object sender, System.EventArgs e)
{
    if(SelectionList1.Selection == null)
    {
        Label2.Text = "You did not select a person";
    }
    else
    {
        Person selectedPerson = customers[SelectionList1.SelectedIndex];

        Label2.Text = selectedPerson.Name 
            + " (aka " + selectedPerson.Nickname + "),"
            + " initials " + selectedPerson.Initials;
    }
}

Adding Items to a List

A List control contains a collection of items in the MobileListItem class. There are a number of ways that you can provide a List control with items:

  • You can declare static <Item> elements inside a list. Each <Item> element becomes an item in the list, and the item's properties are set from attributes of the <Item> element.
  • You can programmatically add items to the list by using the control's Items collection. You can construct a MobileListItem and add it to the collection before rendering.
  • You can bind data in a List control to a collection (an object that implements ICollection or IListSource).

See Also

Accessing Data Using Listing Controls | List | ObjectList | Accessing Data with ASP.NET | Accessing Data with ADO.NET | Inserting Data Into a SQL Database | Developing Mobile Web Applications | Application Developer's Guide