次の方法で共有


SelectionList クラスと List クラスとの相違点

更新 : 2007 年 11 月

SelectionList コントロールと List コントロールは似ていますが、デザイン時または実行時の機能に基本的な違いがあります。これらのクラスは、いずれもリスト項目のコレクションを保持します。ただし、List コントロールが PagedControl および最終的に MobileControl クラスから派生している一方で、SelectionList コントロールは MobileControl クラスから直接派生しており、改ページ調整を処理するプロパティ (ItemWeight プロパティなど) を持っていません。

これらのクラスの主な違いは、SelectionList クラスが単一項目または複数項目の選択をサポートしている点です。SelectType プロパティには、ListSelectType の列挙値があります。この値によって、SelectionList が単一の選択モードであるか複数の選択モードであるかが決定します。

List クラスでは、リストで 1 つの項目しか選択できません。これに対し、SelectionList クラスではさまざまなリスト型を指定できます。これには、CheckBoxDropDownListBoxMultiSelectListBox、および Radio の各リスト型があります。

SelectionList の機能

SelectType プロパティを以下のいずれかに設定すると、SelectionList コントロールは単一の選択モードになります。

選択の処理

単一の選択モードになっているときに、SelectionList コントロールで現在選択されている項目を取得するには、SelectedIndex プロパティおよび Selection プロパティを使用します。

CheckBox 列挙値および MultiSelectListBox 列挙値は、複数選択モードを示しています。選択内容を取得するには、各項目の Selected プロパティを照会します。

次の例は、複数選択リストから選択された値を取得する方法を示しています。

<%@ 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>

List コントロールへの項目の追加

List コントロールには、MobileListItem クラス内の項目のコレクションが含まれています。List コントロールに項目を追加するには、いくつかの方法があります。

  • リスト内に <Item> 要素を作成します。各 <Item> 要素は、リスト内で MobileListItem となり、そのプロパティは <Item> 要素の属性から設定されます。

  • プログラムで List コントロールの Items コレクションを使用して、リストに項目を追加します。レンダリングの前に MobileListItem オブジェクトを構築してコレクションに追加できます。

  • List コントロールをデータにバインドします。特に、IEnumerable インターフェイスや IListSource インターフェイスを実装する ArrayList オブジェクトまたは DataSet オブジェクトなどがバインド先となります。

参照

概念

リスト コントロールによるデータへのアクセス

参照

List

ObjectList

その他の技術情報

ASP.NET でのデータ アクセス

ASP.NET モバイル Web ページの作成

アプリケーション開発者ガイド