Evento SelectedIndexChanged

Si verifica ogni volta che viene modificata la selezione di un oggetto SelectionList.

public event EventHandler SelectedIndexChanged

Osservazioni

La modifica della selezione nel client non genera un evento di postback. L'evento viene chiamato nel server nel caso in cui venga generato un evento di postback da un altro controllo e venga modificata la selezione.

Esempio

Nell'esempio riportato di seguito viene mostrato come intercettare un evento SelectedIndexChanged quale risultato di un postback da un controllo Command. Il valore dell'elemento selezionato viene mostrato con un controllo Label mediante l'evento SelectedIndexChanged gestito.

[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>

Vedere anche

Metodo OnSelectedIndexChanged | Classe SelectionList