Share via


ItemCommand Event (Command)

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

public event CommandEventHandler ItemCommand

Remarks

When an ItemCommand event handler is defined, the Command control notifies the handler when an item event is generated through user interaction. Unlike the Click event, the ItemCommand event is bubbled up to the parent controls.

The ItemCommand event rendering is device-specific and is discussed in detail in the Device-Specific Behavior section of the Command control.

The OnItemCommand event is raised after an OnClick event. In a scenario where you repeat the same set of actions each time a command button is clicked, you can use either the CommandName or CommandArgument property of the Command control to identify which command button the user clicked.

Example

The following example demonstrates how to trap ItemCommand events for multiple commands.

<script language="vb" runat=server>

Sub cmd_OnItemCommand(sender As Object, e As CommandEventArgs)
   ' Use the value of the CommandName property to find
   ' a command button that has been clicked.
   message1.Text = "Today's quote of " + e.CommandName + _ 
   " is " + e.CommandArgument   

   message2.Text = "Yesterday's quote of " + e.CommandName + _
   " was " + (Convert.ToInt32(e.CommandArgument)-5)
End Sub

</script>

<mobile:form id="myForm" runat=server>
   <mobile:label id="message1" runat=server>Click the button for
      quotes</mobile:label> 
   <mobile:label id="message2" runat=server></mobile:label> 
   <mobile:command id="CmdA" onItemCommand ="cmd_OnItemCommand"
      CommandArgument="70" CommandName="ca" 
      runat="server" Text="Company A" />
   <mobile:command id="CmdB" onItemCommand ="cmd_OnItemCommand"
      CommandArgument="25" CommandName="cb" 
      runat="server" Text="Company B" />
   <mobile:command id="CmdC" OnItemCommand ="cmd_OnItemCommand"
      CommandArgument="110" CommandName="cc" 
      runat="server" Text= "Company C" />
</mobile:form>
[C#]

<script language="c#" runat=server>

void cmd_OnItemCommand(object sender, CommandEventArgs e)
{
   // Use the value of the CommandName property to find
   // a command button that has been clicked.
   message1.Text = "Today's quote of " + e.CommandName + 
   " is " + e.CommandArgument;   

   message2.Text = "Yesterday's quote of " + e.CommandName +
   " was " + (Convert.ToInt32(e.CommandArgument)-5);
}

</script>

<mobile:form id="myForm" runat=server>
   <mobile:label id="message1" runat=server>Click the button for
      quotes</mobile:label> 
   <mobile:label id="message2" runat=server></mobile:label> 
   <mobile:command id="CmdA" onItemCommand ="cmd_OnItemCommand"
      CommandArgument="70" CommandName="ca" 
      runat="server" Text="Company A" />
   <mobile:command id="CmdB" onItemCommand ="cmd_OnItemCommand"
      CommandArgument="25" CommandName="cb" 
      runat="server" Text="Company B" />
   <mobile:command id="CmdC" OnItemCommand ="cmd_OnItemCommand"
      CommandArgument="110" CommandName="cc" 
      runat="server" Text= "Company C" />
</mobile:form>

See Also

ItemCommand Event (List) | ListCommandEventArgs Class | ItemCommand Event (ObjectList) | ObjectListCommandEventArgs Class | OnItemCommand Method

Command Class