List.ItemCommand Event

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

Namespace:  System.Web.UI.MobileControls
Assembly:  System.Web.Mobile (in System.Web.Mobile.dll)

public event ListCommandEventHandler ItemCommand
<asp:List OnItemCommand="ListCommandEventHandler" />

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.

The following code example shows how to use the ItemCommand event to call a method that changes the status of an item in the list and recalculates the status totals. This example is part of a larger example for the List overview.

private void Status_ItemCommand(object sender, 
    ListCommandEventArgs e)
{
    const string spec = "You now have {0} " + 
        "tasks done, {1} tasks scheduled, and " +
        "{2} tasks pending.";

    // Move selection to next status toward 'done'
    switch (e.ListItem.Value)
    {
        case "scheduled":
            schedCount -= 1;
            pendCount += 1;
            e.ListItem.Value = "pending";
            break;
        case "pending":
            pendCount -= 1;
            doneCount += 1;
            e.ListItem.Value = "done";
            break;
    }

    // Show the status of the current task
    Label1.Text = e.ListItem.Text + " is " +
        e.ListItem.Value;

    // Show current selection counts
    Label2.Text = String.Format(spec, doneCount, 
        schedCount, pendCount);
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft