Differences Between the SelectionList and List Classes
Although the SelectionList and List controls are similar, there are fundamental difference in functionality at design time and run time. Both classes maintain a collection of list items. However, whereas the List control is derived from the PagedControl 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 supports 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. In contrast, the SelectionList class enables you to specify a variety of list types, including CheckBox, DropDown, ListBox, MultiSelectListBox, and Radio.
SelectionList Functionality
A SelectionList control is in single selection mode when you set the SelectType property to any of the following:
Handling the Selection
To retrieve the current selected item in a SelectionList control while in single selection mode, use the SelectedIndex and Selection properties.
The CheckBox and MultiSelectListBox enumerated values indicate multiselect mode. To retrieve the selection, query each item's Selected property.
The following example shows how to retrieve the selected values from a multiselect list.
<%@ Page Language="VB"
Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<script runat="server">
private class Person
' Private Fields
Private _Name, _Nickname As String
' Constructor
Public Sub New(ByVal name As String, _
ByVal nickname As String)
Me._Name = name
Me._Nickname = nickname
End Sub
' Public Properties
Public ReadOnly Property Name()
Get
Return _Name
End Get
End Property
Public ReadOnly Property Nickname()
Get
Return _Nickname
End Get
End Property
End Class
' An ArrayList for the Person objects
Dim presidents = New ArrayList()
Private Sub Page_Load(ByVal sender As Object, _
ByVal e As EventArgs)
' Fill the presidents ArrayList
presidents.Add( _
New Person("George Washington", "George"))
presidents.Add( _
New Person("Abraham Lincoln", "Abe"))
presidents.Add( _
New Person("Theodore Roosevelt", "Teddy"))
If Not IsPostBack Then
' Bind the array to the list.
SelectionList1.DataSource = presidents
' Specify the field to display
SelectionList1.DataValueField = "Name"
SelectionList1.DataBind()
End If
End Sub
Protected Sub Command1_Click( _
ByVal sender As Object, ByVal e As EventArgs)
Dim retval As String = String.Empty
Dim per As Person
If Not SelectionList1.IsMultiSelect Then
retval = "Value: "
' Get the selected item
per = CType(presidents(SelectionList1.SelectedIndex), Person)
' Get the text of the item
If Not IsNothing(per) Then
retval &= per.Name & "(" & per.Nickname & ")"
End If
ElseIf SelectionList1.IsMultiSelect Then
retval = "Values: "
' Gather the text from list items
Dim li As MobileListItem
Dim i As Integer = 0
For i = 0 To SelectionList1.Items.Count - 1
li = SelectionList1.Items(i)
' Gather text only from selected items
If li.Selected Then
per = CType(presidents(li.Index), Person)
retval &= per.Name & "(" & per.Nickname & "), "
End If
Next
End If
' Clean ending comma, if any
If retval.IndexOf(", ") > -1 Then
retval = retval.Substring(0, retval.Length - 2)
End If
' Put return value into the Label
Label1.Text = retval
' Activate Form2
Me.ActiveForm = Form2
End Sub
Protected Sub Command2_Click( _
ByVal sender As Object, ByVal e As EventArgs)
' Activate Form1
Me.ActiveForm = Form1
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<mobile:form id="Form1" runat="server">
Select several items in the list:<br />
<mobile:SelectionList ID="SelectionList1"
Runat="server" SelectType="Checkbox">
</mobile:SelectionList>
<mobile:Command ID="Command1" Runat="server"
OnClick="Command1_Click">
Record Choices
</mobile:Command>
</mobile:form>
<mobile:Form ID="Form2" Runat="server">
<mobile:Label ID="Label1" runat="server" />
<mobile:Command ID="Command2" Runat="server"
OnClick="Command2_Click">Return
</mobile:Command>
</mobile:Form>
</body>
</html>
<%@ Page Language="C#"
Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<script runat="server">
private class Person
{
// Private Fields
private String _Name, _Nickname;
// Constructor
public Person(string name, string nickname)
{
this._Name = name;
this._Nickname = nickname;
}
// Public Properties
public String Name { get { return _Name; } }
public String Nickname { get { return _Nickname; } }
}
// An ArrayList for the Person objects
ArrayList presidents = new ArrayList();
private void Page_Load(object sender, System.EventArgs e)
{
// Fill the Person object
presidents.Add(
new Person("George Washington", "George"));
presidents.Add(
new Person("Abraham Lincoln", "Abe"));
presidents.Add(
new Person("Theodore Roosevelt", "Teddy"));
if (!IsPostBack)
{
// Bind the array to the list.
SelectionList1.DataSource = presidents;
// Specify the field to display
SelectionList1.DataValueField = "Nickname";
SelectionList1.DataBind();
}
}
protected void Command1_Click(object sender, EventArgs e)
{
string retval = String.Empty;
Person per;
if (!SelectionList1.IsMultiSelect)
{
retval = "Value: ";
// Get the selected item
per = (Person)presidents[SelectionList1.SelectedIndex];
// Get the name and nickname of the person
if (per != null)
retval += per.Name + " (" + per.Nickname + ")";
}
else if (SelectionList1.IsMultiSelect)
{
retval = "Values: ";
// Gather the text from list items
foreach (MobileListItem li in SelectionList1.Items)
{
// Gather text only from selected items
if (li.Selected)
{
per = (Person)presidents[li.Index];
retval += per.Name + " (" + per.Nickname + "), ";
}
}
}
// Clean ending comma, if any
if (retval.IndexOf(", ") > -1)
retval = retval.Substring(0, retval.Length - 2);
// Put return value into the Label
Label1.Text = retval;
// Activate Form2
this.ActiveForm = Form2;
}
protected void Command2_Click(object sender, EventArgs e)
{
// Activate Form1
this.ActiveForm = Form1;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
<mobile:form id="Form1" runat="server">
Select several items in the list:<br />
<mobile:SelectionList ID="SelectionList1"
Runat="server" SelectType="Checkbox">
</mobile:SelectionList>
<mobile:Command ID="Command1" Runat="server"
OnClick="Command1_Click">
Record Choices
</mobile:Command>
</mobile:form>
<mobile:Form ID="Form2" Runat="server">
<mobile:Label ID="Label1" runat="server" />
<mobile:Command ID="Command2" Runat="server"
OnClick="Command2_Click">Return
</mobile:Command>
</mobile:Form>
</body>
</html>
Adding Items to a List Control
A List control contains a collection of items in the MobileListItem class. There are a number of ways that you can add items to a List control:
Create <Item> elements inside a list. Each <Item> element becomes a MobileListItem in the list, and its properties are set from attributes of the <Item> element.
Programmatically add items to the list using the List control's Items collection. You can construct a MobileListItem object and add it to the collection before rendering.
Bind the List control to data, specifically to any object that implements the IEnumerable interface or the IListSource interface, such as ArrayList or DataSet objects.
See Also
Reference
Concepts
Accessing Data Using Listing Controls