This documentation is archived and is not being maintained.

ImageButton.Command Event

Occurs when the ImageButton is clicked.

[Visual Basic]
Public Event Command As CommandEventHandler
[C#]
public event CommandEventHandler Command;
[C++]
public: __event CommandEventHandler* Command;

[JScript] In JScript, you can handle the events defined by a class, but you cannot define your own.

Event Data

The event handler receives an argument of type CommandEventArgs containing data related to this event. The following CommandEventArgs properties provide information specific to this event.

Property Description
CommandArgument Gets the argument for the command.
CommandName Gets the name of the command.

Remarks

The Command event is raised when an ImageButton control is clicked.

Note   The Command event is raised through the control hierarchy in the form of the BubbleEvent.
Note   This event causes the page to post back to the server.

For more information about handling events, see Consuming Events.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to specify and code a handler for the Command event to determine which ImageButton control is clicked.

[Visual Basic] 
<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>

   <script language="VB" runat="server">

      Sub ImageButton_Command(sender As Object, e As CommandEventArgs) 
         If (e.CommandName = "Sort") And (e.CommandArgument = "Ascending") Then
            Label1.Text = "You clicked the Sort Ascending Button"
         Else
            Label1.Text = "You clicked the Sort Descending Button"
         End If
      End Sub

   </script>

</head>

<body>

   <form runat="server">

      <h3>ImageButton CommandName Sample</h3>

      Click an image.<br><br>

      <asp:ImageButton id="imagebutton1" runat="server"
           AlternateText="Sort Ascending"
           ImageUrl="images/pict.jpg"
           OnCommand="ImageButton_Command"
           CommandName="Sort"
           CommandArgument="Ascending"/>

      <asp:ImageButton id="imagebutton2" runat="server"
           AlternateText="Sort Descending"
           ImageUrl="images/pict2.jpg"
           OnCommand="ImageButton_Command"
           CommandName="Sort"
           CommandArgument="Descending"/>

      <br><br>
    
      <asp:label id="Label1" runat="server"/>

   </form>

</body>
</html>


[C#] 
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>

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

      void ImageButton_Command(object sender, CommandEventArgs e) 
      {
         if (e.CommandName == "Sort" && e.CommandArgument == "Ascending")
            Label1.Text = "You clicked the Sort Ascending Button";
         else
            Label1.Text = "You clicked the Sort Descending Button";
      }

   </script>

</head>

<body>

   <form runat="server">

      <h3>ImageButton CommandName Sample</h3>

      Click an image.<br><br>

      <asp:ImageButton id="imagebutton1" runat="server"
           AlternateText="Sort Ascending"
           ImageUrl="images/pict.jpg"
           OnCommand="ImageButton_Command"
           CommandName="Sort"
           CommandArgument="Ascending"/>

      <asp:ImageButton id="imagebutton2" runat="server"
           AlternateText="Sort Descending"
           ImageUrl="image/pict2.jpg"
           OnCommand="ImageButton_Command"
           CommandName="Sort"
           CommandArgument="Descending"/>

      <br><br>
    
      <asp:label id="Label1" runat="server"/>

   </form>

</body>
</html>


[JScript] 
<%@ Page Language="JScript" AutoEventWireup="True" %>
<html>
<head>

   <script language="JSCRIPT" runat="server">

      function ImageButton_Command(sender : Object, e : CommandEventArgs){
         if(e.CommandName == "Sort" && e.CommandArgument == "Ascending")
            Label1.Text = "You clicked the Sort Ascending Button"
         else
            Label1.Text = "You clicked the Sort Descending Button"
      }

   </script>

</head>

<body>

   <form runat="server">

      <h3>ImageButton CommandName Sample</h3>

      Click an image.<br><br>

      <asp:ImageButton id="imagebutton1" runat="server"
           AlternateText="Sort Ascending"
           ImageUrl="images/pict.jpg"
           OnCommand="ImageButton_Command"
           CommandName="Sort"
           CommandArgument="Ascending"/>

      <asp:ImageButton id="imagebutton2" runat="server"
           AlternateText="Sort Descending"
           ImageUrl="images/pict2.jpg"
           OnCommand="ImageButton_Command"
           CommandName="Sort"
           CommandArgument="Descending"/>

      <br><br>
    
      <asp:label id="Label1" runat="server"/>

   </form>

</body>
</html>

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

ImageButton Class | ImageButton Members | System.Web.UI.WebControls Namespace | OnCommand | CommandEventHandler

Show: