Compartir a través de


ImageButton Web Server Control

Enables you to handle user clicks in an image, which gives you functionality similar to an image map.

<asp:ImageButton id="ImageButton1" 
     ImageUrl="string"
     Command="Command"
     CommandArgument="CommandArgument"
     CausesValidation="true | false"
     OnClick="OnClickMethod"
     runat="server"/>

Remarks

Use the ImageButton control to display an image that responds to mouse clicks. Specify the image to display in the control by setting the ImageUrl property.

Both the Click and Command events are raised when the ImageButton control is clicked.

By using the OnClick event handler, you can programmatically determine the coordinates where the image is clicked. You can then code a response based on the values of the coordinates. Note the origin (0, 0) is located at the upper-left corner of the image.

You can use the OnCommand event handler 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 same Web page. The value of the CommandName 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   Because the <asp:ImageButton> element has no content, you can close the tag with /> instead of using a separate closing tag.

By default, page validation is performed when an ImageButton control is clicked. Page validation determines whether the input controls associated with a validation control on the page pass the validation rules specified by the validation control. If you have a ImageButton control that needs to disable this behavior, such as a reset button, set the CausesValidation property to false.

For detailed information on the ImageButton Web server control's properties and events, see the ImageButton Class documentation.

Example

The following example demonstrates how to use an ImageButton control to display the coordinates of the mouse pointer when the pointer is clicked on the image.

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      Sub ImageButton_Click(sender As Object, e As ImageClickEventArgs) 
         Label1.Text="You clicked the ImageButton control at the " & _
                     "Coordinates: (" & e.X.ToString() & ", " & _
                     e.Y.ToString() & ")"
      End Sub
   </script>
</head>
<body>
   <form runat="server">
      <h3>ImageButton Sample</h3>
      Click anywhere on the image.<br><br>
      <asp:ImageButton id="imagebutton1" runat="server"
           AlternateText="ImageButton 1"
           ImageAlign="left"
           ImageUrl="images\pict.jpg"
           OnClick="ImageButton_Click"/>
      <br><br>
      <asp:label id="Label1" runat="server"/>
   </form>
</body>
</html>
[C#]
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
<head>
   <script runat="server">
      void ImageButton_Click(object Source, ImageClickEventArgs e) 
      {
         Label1.Text="You clicked the ImageButton control at the " +
                     "Coordinates: (" + e.X.ToString() + ", " +
                     e.Y.ToString() + ")";
      }
   </script>
</head>
<body>
   <form runat="server">
      <h3>ImageButton Sample</h3>
      Click anywhere on the image.<br><br>
      <asp:ImageButton id="imagebutton1"
           AlternateText="ImageButton 1"
           ImageAlign="left"
           ImageUrl="images\pict.jpg"
           OnClick="ImageButton_Click"
           runat="server"/>
      <br><br>
      <asp:Label id="Label1" 
           runat="server"/>
   </form>
</body>
</html>

See Also

Web Server Controls | ImageButton Class