This documentation is archived and is not being maintained.

ImageButton.OnCommand Method

Raises the Command event and allows you to handle the Command event directly.

[Visual Basic]
Protected Overridable Sub OnCommand( _
   ByVal e As CommandEventArgs _
)
[C#]
protected virtual void OnCommand(
 CommandEventArgs e
);
[C++]
protected: virtual void OnCommand(
 CommandEventArgs* e
);
[JScript]
protected function OnCommand(
   e : CommandEventArgs
);

Parameters

e
A CommandEventArgs that contains the event data.

Remarks

The Command event is raised when the ImageButton control is clicked. The OnCommand event handler is used to make the ImageButton control behave like a command button. A command name can be associated with the control by using the CommandName property. This allows multiple ImageButton controls to be placed on the Web page. The value in this property can then be programmatically identified in the OnCommand event handler to determine the appropriate action to perform when each ImageButton control is clicked. The CommandArgument property can also be used to pass additional information about the command, such as specifying ascending order.

Note   The Command event is raised through the control hierarchy in the form of the BubbleEvent.

Raising an event invokes the event handler through a delegate. For more information, see Raising an Event.

The OnCommand method also allows derived classes to handle the event without attaching a delegate. This is the preferred technique for handling the event in a derived class.

Notes to Inheritors:  When overriding OnCommand in a derived class, be sure to call the base class's OnCommand method so that registered delegates receive the event.

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

Show: