利用集合元素创建列表对象

更新:2007 年 11 月

下面的步骤描述移动 List 控件如何利用集合中的元素构造一个 MobileListItem 实例:

  1. 该控件检查是否已定义 DataTextFieldDataValueField 属性。如果已定义,该控件将使用这些字段名来发现元素中的属性并设置 MobileListItem 实例的 TextValue 属性。

  2. 如果既没有定义 DataTextField 属性也没有定义 DataValueField 属性,则控件将 MobileListItem 实例的 TextValue 属性设置为元素的字符串表示形式(通过使用 ToString 方法)。

  3. 如果定义了 ItemDataBind 事件处理程序,则调用该处理程序。可以使用该处理程序来设置 MobileListItem 实例的属性。

提供默认呈现时,列表控件将以其 Text 属性表示 MobileListItem 实例。在模板化呈现中,模板可以呈现 MobileListItem 实例(或关联的数据绑定对象)的所需属性。

如果已设置 ItemsAsLinks 属性,List 控件会将项呈现为超链接。Text 属性的值将成为链接文本,而 Value 属性的值则成为目标 URL。

处理选择

如果列表很复杂(如对象数组),则无法通过选择直接访问选定项的成员。不过,如果适当地设计应用程序,则可以访问关联的对象。如果您正在创建一组对象来填充列表,则可以使该组成为全局数组,然后将返回值作为该数组的索引进行处理,如下面的代码示例所示。

<%@ 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 customers(3) As Person

    Private Class Person
        Private _Name, _Nickname, _Initials As String

        Public Sub New(ByVal name As String, _
            ByVal nickname As String, ByVal initials As String)
            Me._Name = name
            Me._Nickname = nickname
            Me._Initials = initials
        End Sub

        Public ReadOnly Property Name() As String
            Get
                Return _Name
            End Get
        End Property
        Public ReadOnly Property Nickname() As String
            Get
                Return _Nickname
            End Get
        End Property
        Public ReadOnly Property Initials() As String
            Get
                Return _Initials
            End Get
        End Property
    End Class

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        ReDim customers(2)
        customers(0) = New Person("George Washington", "George", "GW")
        customers(1) = New Person("Abraham Lincoln", "Abe", "AL")
        customers(2) = New Person("Theodore Roosevelt", "Teddy", "TR")

        If (Not IsPostBack) Then
            ' Bind the array to the list.
            List1.DataSource = customers
            List1.DataTextField = "Name"
            List1.DataBind()
        End If
    End Sub

    Protected Sub List1_ItemCommand(ByVal sender As Object, _
        ByVal e As ListCommandEventArgs)

        Dim selectedPerson As Person = customers(e.ListItem.Index)
        Label1.Text = String.Format("{0} (AKA {1}), initials {2}", _
            selectedPerson.Name, selectedPerson.Nickname, _
            selectedPerson.Initials)

        ActiveForm = Form2
    End Sub

    Protected Sub Command1_Click(ByVal sender As Object, _
        ByVal e As EventArgs)

        Me.ActiveForm = Me.Form1
    End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
    <mobile:form id="Form1" runat="server">
        <mobile:List ID="List1" Runat="server" OnItemCommand="List1_ItemCommand">
        </mobile:List>
    </mobile:form>
    <mobile:Form ID="Form2" Runat="server">
        <mobile:Label ID="Label1" runat="server">Label</mobile:Label>
        <mobile:Command ID="Command1" Runat="server" OnClick="Command1_Click">Return to Form1</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 Person[] customers = new Person[3];

    private 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)
    {
        customers[0] = new Person("George Washington", "George", "GW");
        customers[1] = new Person("Abraham Lincoln", "Abe", "AL");
        customers[2] = new Person("Theodore Roosevelt", "Teddy", "TR");

        if(!IsPostBack)
        {
            // Bind the array to the list.
            List1.DataSource    = customers;
            List1.DataTextField = "Name";
            List1.DataBind();
        }
    }

    private void List1_ItemCommand(object sender, 
        ListCommandEventArgs e)
    {
        Person selectedPerson = customers[e.ListItem.Index];
        Label1.Text = String.Format("{0} (AKA {1}), initials {2}", 
            selectedPerson.Name, selectedPerson.Nickname,
            selectedPerson.Initials);

        ActiveForm = Form2;
    }

    protected void Command1_Click(object sender, EventArgs e)
    {
        this.ActiveForm = this.Form1;
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
    <mobile:form id="Form1" runat="server">
        <mobile:List ID="List1" Runat="server" OnItemCommand="List1_ItemCommand">
        </mobile:List>
    </mobile:form>
    <mobile:Form ID="Form2" Runat="server">
        <mobile:Label ID="Label1" runat="server">Label</mobile:Label>
        <mobile:Command ID="Command1" Runat="server" OnClick="Command1_Click">Return to Form1</mobile:Command>
    </mobile:Form>
</body>
</html>

请参见

概念

使用列表控件访问数据