ImageButton.CommandName Property
Gets or sets the command name associated with the ImageButton control.
[Visual Basic] Public Property CommandName As String [C#] public string CommandName {get; set;} [C++] public: __property String* get_CommandName(); public: __property void set_CommandName(String*); [JScript] public function get CommandName() : String; public function set CommandName(String);
Property Value
The command name associated with the ImageButton control. The default value is String.Empty.
Remarks
Use this property to specify the command to perform when the ImageButton control is clicked, such as Sort, Cancel, and Edit. This allows multiple ImageButton controls to be placed on the same 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.
Supplemental information about the command, such as specifying ascending sort order, can be included by using the CommandArgument property.
Example
[Visual Basic, C#, JScript] The following example demonstrates how to use the CommandName property to associate a command name with an ImageButton control.
[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" Then Label1.Text = "You clicked the Sort Button" Else Label1.Text = "You clicked the Edit 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" ImageUrl="images/pict1.jpg" OnCommand="ImageButton_Command" CommandName="Sort"/> <asp:ImageButton id="imagebutton2" runat="server" AlternateText="Edit" ImageUrl="images/pict2.jpg" OnCommand="ImageButton_Command" CommandName="Edit"/> <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") Label1.Text = "You clicked the Sort Button"; else Label1.Text = "You clicked the Edit 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" ImageUrl="images/pict1.jpg" OnCommand="ImageButton_Command" CommandName="Sort"/> <asp:ImageButton id="imagebutton2" runat="server" AlternateText="Edit" ImageUrl="images/pict2.jpg" OnCommand="ImageButton_Command" CommandName="Edit"/> <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") Label1.Text = "You clicked the Sort Button" else Label1.Text = "You clicked the Edit 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" ImageUrl="images/pict1.jpg" OnCommand="ImageButton_Command" CommandName="Sort"/> <asp:ImageButton id="imagebutton2" runat="server" AlternateText="Edit" ImageUrl="images/pict2.jpg" OnCommand="ImageButton_Command" CommandName="Edit"/> <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
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 | CommandArgument | OnClick