共用方式為


SelectionList 和 List 類別之間的差異

更新:2007 年 11 月

雖然 SelectionListList 控制項非常相似,兩者在設計階段和執行階段的功能上仍有基本上的差異。這兩個類別都維護清單項目的集合。然而,其中 List 控制項衍生自 PagedControl,而且最終衍生自 MobileControl 類別;而 SelectionList 控制項是直接衍生自 MobileControl 類別,而且沒有重新編頁的處理屬性 (例如,ItemWeight 屬性)。

這些類別的主要差異在於 SelectionList 類別可支援單一或多重選取的項目。SelectType 屬性包含 ListSelectType 列舉值,這個值會決定 SelectionList 是在單一或多重選取模式。

使用 List 類別時,您只能在清單中選擇單一個項目。相對地,SelectionList 類別可讓您指定多種清單類型,其中包括 CheckBoxDropDownListBoxMultiSelectListBoxRadio

SelectionList 功能

當您將 SelectType 屬性設定為下列任何一項時,SelectionList 控制項就會在單一選取模式下:

處理選取項目

若要在單一選取模式下擷取 SelectionList 控制項中的目前選取項目,請使用 SelectedIndexSelection 屬性。

CheckBoxMultiSelectListBox 列舉值,表示為多重選取模式。若要擷取選取項目,請查詢每個項目的 Selected 屬性。

下列程式碼範例示範如何從多重選取清單中擷取選取的值。

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

將項目加入至清單控制項

List 控制項包含 MobileListItem 類別中項目的集合。您可以用數種方法將項目加入至 List 控制項:

  • 在清單內部建立 <Item> 項目。每個 <Item> 項目都會成為清單中的 MobileListItem,而且其屬性 (Property) 會根據 <Item> 項目的屬性 (Attribute) 設定。

  • 以程式設計方式,將項目加入至使用 List 控制項之 Items 集合的清單中。您可以建構 MobileListItem 物件,並在呈現之前先將其加入至集合。

  • List 控制項繫結至資料,尤其是實作 IEnumerable 介面或 IListSource 介面的任何物件,例如 ArrayListDataSet 物件。

請參閱

概念

使用列出的控制項存取資料

參考

List

ObjectList

其他資源

使用 ASP.NET 存取資料

開發 ASP.NET Mobile Web 網頁

應用程式開發人員手冊