Evento ItemCommand (List)

Si verifica quando un utente sceglie un comando associato a un controllo List.

public event ListCommandEventHandler ItemCommand

Osservazioni

Quando si esegue il rendering di un elenco mediante l'utilizzo di modelli, il gestore eventi ItemCommand viene chiamato tramite il meccanismo di bubbling degli eventi di ASP.NET. Al gestore degli eventi viene passato un argomento di tipo ListCommandEventArgs che contiene informazioni sull'elemento di origine e la proprietà CommandName del controllo che ha generato l'evento. In questo modo è possibile eseguire il rendering di un solo elemento dell'elenco con più interazioni associate.

Durante il rendering predefinito, il controllo rende disponibile un'interfaccia utente di base che consente all'utente di fare clic sulle voci di un elenco. Durante il postback, il gestore eventi ItemCommand viene richiamato con un argomento di tipo ListCommandEventArgs, che contiene informazioni sull'elemento di origine. La proprietà CommandName dell'oggetto è null.

Esempio

Nell'esempio seguente viene mostrato come rilevare l'evento ItemCommand per associare un controllo List a una matrice e riempire il controllo List con il contenuto della matrice. È inoltre possibile associare il controllo List a un insieme di dati compilato con il risultato di una query SQL. In quest'ultimo caso, è possibile impostare le proprietà DataTextField e DataValueField sui nomi di colonna, che fungono da proprietà Text e Value degli elementi dell'insieme MobileListItemCollection.

<script Language="vb" runat="server">
Public Class Task
   Private _TaskName, _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

Public Sub Page_Load(sender As Object, e As EventArgs)
   If Not IsPostBack
      List1.DataValueField = "Status"
      List1.DataTextField = "TaskName"
      Dim arr As New ArrayList()
      arr.Add (New Task ("Verify transactions", "done"))
      arr.Add (New Task ("Check balance sheet", "scheduled"))
      arr.Add (New Task ("Send report", "pending"))
      List1.DataSource = arr
      List1.DataBind ()
   End If
End Sub

Sub ShowStatus (sender As Object, e As ListCommandEventArgs) 
   Label1.Text = e.ListItem.Text + " is " + e.ListItem.Value
End Sub
</script>

<mobile:Form runat="server" id="Form1" >
   <mobile:List runat="server" id="List1" 
      OnItemCommand="ShowStatus" />
   <mobile:Label runat="server" id="Label1" ForeColor=green 
      Font-Italic=true />
</mobile:Form>
[C#]

<script Language="c#" runat="server">
class Task
{
   private String _TaskName, _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)
   {
      List1.DataValueField="Status";
      List1.DataTextField="TaskName";
      ArrayList arr = new ArrayList();
      arr.Add (new Task ("Verify transactions", "done"));
      arr.Add (new Task ("Check balance sheet", "scheduled"));
      arr.Add (new Task ("Send report", "pending"));
      List1.DataSource = arr;
      List1.DataBind ();
   }
}

void ShowStatus(Object sender, ListCommandEventArgs e)
{ 
   Label1.Text = e.ListItem.Text+ " is " + e.ListItem.Value;
}
</script>

<mobile:Form runat="server" id="Form1" >
   <mobile:List runat="server" id="List1" 
      OnItemCommand="ShowStatus" />
   <mobile:Label runat="server" id="Label1" ForeColor=green 
      Font-Italic=true />
</mobile:Form>

Vedere anche

Classe Command | Classe CommandEventArgs (Command) | Evento ItemCommand (ObjectList) | Classe ObjectListCommandEventArgs | Metodo OnItemCommand | Classe List