從集合元素建立清單物件
更新:2007 年 11 月
下列步驟描述行動 List 控制項如何從集合中的項目建構 MobileListItem 執行個體 (Instance):
控制項會檢查是否已定義 DataTextField 或 DataValueField 屬性。如果已定義這些屬性,則控制項會使用這些欄位名稱來探索項目的屬性,並設定 MobileListItem 執行個體的 Text 和 Value 屬性。
如果 DataTextField 或 DataValueField 屬性都沒有定義,控制項便會將 MobileListItem 執行個體的 Text 和 Value 屬性設定為該項目的字串表示 (使用 ToString 方法)。
如果有定義 ItemDataBind 事件處理常式,便會呼叫該處理常式。您可以使用此處理常式設定 MobileListItem 執行個體的屬性。
當提供預設呈現時,清單控制項便會以其 Text 屬性來表示 MobileListItem 執行個體。在樣板化呈現中,樣板可呈現 MobileListItem執行個體或是相關聯資料繫結物件所需要的屬性。
如果已設定 ItemsAsLinks 屬性,List 控制項便會將項目呈現為超連結 (Hyperlink)。Text 屬性的值會成為連結文字,而且 Value 屬性的值會成為目標 URL。
處理選取項目
如果您的清單太複雜,如物件陣列,則無法透過選取項目直接存取選取項目的成員。然而,如果您適當設計應用程式,則可存取關聯的物件。如果您正在建立要填入 (Populate) 清單的物件群組,即可讓群組成為全域陣列,然後將傳回的值當做陣列的索引處理,如下列程式碼範例所示。
<%@ Page Language="VB"
Inherits="System.Web.UI.MobileControls.MobilePage" %>
<%@ Register TagPrefix="mobile"
Namespace="System.Web.UI.MobileControls"
Assembly="System.Web.Mobile" %>
<script >
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" >
<mobile:List ID="List1" Runat="server" OnItemCommand="List1_ItemCommand">
</mobile:List>
</mobile:form>
<mobile:Form ID="Form2" Runat="server">
<mobile:Label ID="Label1" >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 >
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" >
<mobile:List ID="List1" Runat="server" OnItemCommand="List1_ItemCommand">
</mobile:List>
</mobile:form>
<mobile:Form ID="Form2" Runat="server">
<mobile:Label ID="Label1" >Label</mobile:Label>
<mobile:Command ID="Command1" Runat="server" OnClick="Command1_Click">Return to Form1</mobile:Command>
</mobile:Form>
</body>
</html>