Partager via


Différences entre les classes SelectionList et List

Mise à jour : novembre 2007

Bien que les contrôles SelectionList et List soient similaires, il existe des différences de fonctionnalités fondamentales, aussi bien au moment du design qu'au moment de l'exécution. Les deux classes gèrent une collection d'éléments de liste. Toutefois, alors que le contrôle List est dérivé de PagedControl et finalement de la classe MobileControl, le contrôle SelectionList est directement dérivé de la classe MobileControl et ne possède pas de propriétés de gestion de pagination, telles que la propriété ItemWeight.

La principale différence entre les classes vient du fait que la classe SelectionList prend en charge la sélection d'éléments uniques ou multiples. La propriété SelectType contient la valeur énumérée ListSelectType, qui détermine si SelectionList est en mode à sélection unique ou multiple.

La classe List vous permet de choisir uniquement des éléments uniques dans une liste. La classe SelectionList, quant à elle, vous permet de spécifier un large éventail de types de listes : CheckBox, DropDown, ListBox, MultiSelectListBox et Radio.

Fonctionnalité de SelectionList

Un contrôle SelectionList est en mode à sélection unique lorsque vous affectez à la propriété SelectType les valeurs ci-dessous :

Gestion de la sélection

Pour récupérer l'élément actuellement sélectionné dans un contrôle SelectionList en mode à sélection unique, utilisez les propriétés SelectedIndex et Selection.

Les valeurs énumérées CheckBox et MultiSelectListBox indiquent le mode à sélection multiple. Pour récupérer la sélection, exécutez une requête sur la propriété Selected de chaque élément.

L'exemple suivant indique comment récupérer les valeurs sélectionnées dans une liste à sélection multiple.

<%@ Page Language="VB" 
    Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile" 
    Namespace="System.Web.UI.MobileControls" 
    Assembly="System.Web.Mobile" %>

<script >
    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" >
        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"  />
        <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 >
    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" >
        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"  />
        <mobile:Command ID="Command2" Runat="server" 
            OnClick="Command2_Click">Return
        </mobile:Command>
    </mobile:Form>
</body>
</html>

Ajout d'éléments à un contrôle List

Un contrôle List contient une collection d'éléments dans la classe MobileListItem. Pour ajouter des éléments à un contrôle List, vous disposez de plusieurs méthodes :

  • Créez des éléments <Item> dans une liste. Chaque élément <Item> devient un MobileListItem dans la liste, et ses propriétés sont définies à partir des attributs de l'élément <Item> .

  • Ajoutez par programme des éléments à la liste à l'aide de la collection Items du contrôle List. Vous pouvez construire un objet MobileListItem et l'ajouter à la collection avant l'exécution du rendu.

  • Liez le contrôle List aux données, notamment à tout objet qui implémente l'interface IEnumerable ou l'interface IListSource, tel qu'un objet ArrayList ou DataSet.

Voir aussi

Concepts

Accès aux données via des contrôles de liste

Référence

List

ObjectList

Autres ressources

Accès aux données avec ASP.NET

Développement de pages Web mobiles de l'ASP.NET

Guide du développeur d'applications