每當使用者變更 SelectionList 物件的選取項目時發生。
public event EventHandler SelectedIndexChanged
備註
變更用戶端上的選取項目不會產生回傳事件。如果其他控制項產生回傳事件並且已變更選取項目,則會在伺服器上呼叫此事件。
範例
下列範例示範如何因為 Command 控制項的回傳而截獲 SelectedIndexChanged 事件。透過處理的 SelectedIndexChanged 事件,使用 Label 控制項顯示所選取項目的值。
[Visual Basic]
<Script language="vb" runat="server">
Shared i, j, k As Integer
Class Task
Private _TaskName As String
Private _Status As String
Public Sub New(TaskName As String, Status As String)
_TaskName = TaskName
_Status = Status
End Sub
Public ReadOnly Property TaskName() As String
Get
Return _TaskName
End Get
End Property
Public ReadOnly Property Status() As String
Get
Return _Status
End Get
End Property
End Class
Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
Dim arr As New ArrayList()
arr.Add(New Task("Verify transactions", "Done"))
arr.Add(New Task("Check balance sheet", "Scheduled"))
arr.Add(New Task("Call customer", "Done"))
arr.Add(New Task("Pay checks", "Pending"))
arr.Add(New Task("Send report", "Pending"))
arr.Add(New Task("Attend meeting", "Scheduled"))
SelectionList1.SelectType = System.Web.UI.MobileControls.ListSelectType.MultiSelectListBox
SelectionList1.Wrapping = System.Web.UI.MobileControls.Wrapping.NoWrap
SelectionList1.DataValueField = "Status"
SelectionList1.DataTextField = "TaskName"
SelectionList1.DataSource = arr
SelectionList1.DataBind()
' Initial setting shows the value of the first
' item from the MobileListItemCollection object
' of SelectionList1.
Label2.Text = "Taskes are arranged by their priority"
End If
End Sub
Sub ShowStatus(sender As Object, e As EventArgs)
'Expanding the selection list's view to see all items in the list.
SelectionList1.Rows = SelectionList1.Items.Count
Label1.Text = SelectionList1.Selection.Text + " is " + SelectionList1.Selection.Value.ToString() + ". Priority of Task is " + (SelectionList1.SelectedIndex + 1).ToString()
End Sub
Sub TaskSummary(sender As Object, e As ListDataBindEventArgs)
Select Case e.ListItem.Value
Case "Done"
i += 1
Case "Scheduled"
j += 1
Case "Pending"
k += 1
End Select
Label2.Text = "You have " + i.ToString() + " tasks done, " + j.ToString() + " tasks scheduled, and " + k.ToString() + " tasks Pending."
End Sub
Sub Form1_Activate (sender As Object, e As EventArgs)
i = 0
j = 0
k = 0
End Sub
<mobile:Form runat="server" id="Form1"
OnActivate="Form1_Activate" Paginate= true>
<mobile:Label runat="server" id="Label1" />
<mobile:Label runat="server" id="Label2" Font-bold=true/>
<mobile:SelectionList runat="server" id="SelectionList1"
OnSelectedIndexChanged="ShowStatus"
OnItemDataBind="TaskSummary"/>
<mobile:Command runat="server" Text="Show Status" />
</mobile:Form>
<script language="c#" runat="server">
static int i, j, k;
class Task
{
private String _TaskName;
private String _Status;
public Task(String TaskName, String Status)
{
_TaskName = TaskName;
_Status = Status;
}
public String TaskName { get { return _TaskName; } }
public String Status { get { return _Status; } }
}
public void Page_Load(Object sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList arr = new ArrayList();
arr.Add (new Task ("Verify transactions", "Done"));
arr.Add (new Task ("Check balance sheet", "Scheduled"));
arr.Add (new Task ("Call customer", "Done"));
arr.Add (new Task ("Pay checks", "Pending"));
arr.Add (new Task ("Send report", "Pending"));
arr.Add (new Task ("Attend meeting", "Scheduled"));
SelectionList1.SelectType =
System.Web.UI.MobileControls.ListSelectType.MultiSelectListBox;
SelectionList1.Wrapping =
System.Web.UI.MobileControls.Wrapping.NoWrap;
SelectionList1.DataValueField = "Status";
SelectionList1.DataTextField = "TaskName"; SelectionList1.DataSource = arr;
SelectionList1.DataBind();
// Initial setting shows the value of the first
// item from the MobileListItemCollection object
// of SelectionList1.
Label2.Text = "Tasks are arranged by their priority";
}
}
void ShowStatus(Object sender, EventArgs e)
{
//Expanding the selection list's view to see all items in the list.
SelectionList1.Rows = SelectionList1.Items.Count;
Label1.Text = SelectionList1.Selection.Text +
" is " +
SelectionList1.Selection.Value +
". Priority of Task is " +
(SelectionList1.SelectedIndex + 1);
}
void TaskSummary(Object sender, ListDataBindEventArgs e)
{
switch (e.ListItem.Value)
{
case "Done":
i++;
break;
case "Scheduled":
j++;
break;
case "Pending":
k++;
break;
Default:
break;
}
Label2.Text = "You have " +
i +
" tasks done, " +
j +
" tasks scheduled, and " +
k +
" tasks Pending.";
}
void Form1_Activate (object sender, EventArgs e)
{
i = j = k = 0;
}
</script>
<mobile:Form runat="server" id="Form1"
OnActivate="Form1_Activate" Paginate= true>
<mobile:Label runat="server" id="Label1" />
<mobile:Label runat="server" id="Label2" Font-bold=true/>
<mobile:SelectionList runat="server" id="SelectionList1"
OnSelectedIndexChanged="ShowStatus"
OnItemDataBind="TaskSummary"/>
<mobile:Command runat="server" Text="Show Status" />
</mobile:Form>