更新:2007 年 11 月
虽然 SelectionList 和 List 控件是相似的,但这两种控件在设计时和运行时都存在根本的功能区别。这两种类都维护着一个列表项集合。但是,尽管 List 控件是从 PagedControl 并且最终是从 MobileControl 类派生的,SelectionList 控件却是直接从 MobileControl 类派生的,并且不具有分页处理属性(如 ItemWeight 属性)。
这两种类之间的主要区别在于,SelectionList 类支持单个或多个选定项。SelectType 属性包含 ListSelectType 枚举值,该值确定 SelectionList 是处于单选模式还是处于多选模式。
使用 List 类,您只能选择列表中的单个项。相比之下,SelectionList 类使您能够指定各种列表类型,包括 CheckBox、DropDown、ListBox、MultiSelectListBox 和 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 控件包含 MobileListItem 类中项的集合。您可以通过多种方式向 List 控件添加项:
在列表中创建 <Item> 元素。每个 <Item> 元素都成为列表中的 MobileListItem,而且其属性 (Property) 是从 <Item> 元素的属性 (Attribute) 设置的。
请使用 List 控件的 Items 集合以编程方式向列表添加项。您可以构造一个 MobileListItem 对象,并在呈现之前将其添加到集合中。
将 List 控件绑定到数据,特别是绑定到实现 IEnumerable 接口或 IListSource 接口的任何对象,例如 ArrayList 或 DataSet 对象。