Share via


ItemCommand Event (List)

Occurs when the user selects a command that is associated with a List control.

public event ListCommandEventHandler ItemCommand

Remarks

When you render a list by using templates, the ItemCommand event handler is called through the event-bubbling mechanism of ASP.NET. The event handler is passed an argument of type ListCommandEventArgs, which contains information about the source item and the CommandName property of the control that generated the event. This allows you to render a single list item with multiple associated interactions.

On default rendering, the control provides a basic user interface (UI) that allows the user to click list items. On postback, the ItemCommand event handler is called with an argument of type ListCommandEventArgs, which contains information about the source item. The CommandName property of this object is null.

Example

The following example demonstrates how to trap the ItemCommand event to bind a List control to an array and fill the List control with the contents of the array. You can also bind the List control to a data set populated with the result of a SQL query. In the latter case, you can set the DataTextField and DataValueField properties to the column names, which act as the Text and Value properties of the items in the MobileListItemCollection collection.

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

See Also

Command Class | CommandEventArgs Class (Command) | ItemCommand Event (ObjectList) | ObjectListCommandEventArgs Class | OnItemCommand Method

List Class