YouTip LogoYouTip

Control Imagebutton

# ASP.NET Web Forms: ImageButton Control The `ImageButton` control is an ASP.NET Web Server Control used to display an image that responds to click events. It functions similarly to a standard `Button` control but uses an image instead of text to trigger a postback or action. Additionally, the `ImageButton` acts as an image map, allowing you to capture the exact X and Y coordinates where the user clicked on the image. --- ## Definition and Usage Use the `ImageButton` control when you want a clickable graphical element on your web page. When clicked, it posts the page back to the server, triggering the `Click` or `Command` event. Because it inherits from the `Image` class, it supports all standard image properties (such as `ImageUrl`, `AlternateText`, and `ImageAlign`) alongside standard button behaviors. --- ## Syntax ```aspx ``` --- ## Properties The table below lists the properties specific to the `ImageButton` control: | Property | Description | .NET Version | | :--- | :--- | :--- | | `CausesValidation` | Specifies whether validation is performed on the page when the `ImageButton` control is clicked. | 1.0 | | `CommandArgument` | An optional argument passed to the `Command` event handler along with the associated `CommandName`. | 1.0 | | `CommandName` | The command name associated with the `ImageButton` control that is passed to the `Command` event. | 1.0 | | `GenerateEmptyAlternateText` | Specifies whether the control generates an empty string (`""`) as the alternate text if no `AlternateText` is provided. | 2.0 | | `OnClientClick` | The name of the client-side JavaScript function to execute when the image is clicked. | 2.0 | | `PostBackUrl` | The URL of the target page to post to from the current page when the `ImageButton` is clicked (Cross-Page Postback). | 2.0 | | `runat` | Specifies that the control is a server-side control. Must be set to `"server"`. | 1.0 | | `TagKey` | Gets the HTML tag that corresponds to this Web server control (renders as ``). | 1.0 | | `ValidationGroup` | The group of controls for which the `ImageButton` causes validation when it posts back to the server. | 2.0 | > **Note:** Since the `ImageButton` control inherits from the `Image` control, all properties of the (control-image.html) (such as `ImageUrl`, `AlternateText`, and `ImageAlign`) are also applicable to the `ImageButton` control. --- ## Standard Web Control Properties The following standard properties are inherited by the `ImageButton` control: ```text AccessKey, Attributes, BackColor, BorderColor, BorderStyle, BorderWidth, CssClass, Enabled, Font, EnableTheming, ForeColor, Height, IsEnabled, SkinID, Style, TabIndex, ToolTip, Width ``` --- ## Standard Control Properties The following basic control properties are also supported: ```text AppRelativeTemplateSourceDirectory, BindingContainer, ClientID, Controls, EnableTheming, EnableViewState, ID, NamingContainer, Page, Parent, Site, TemplateControl, TemplateSourceDirectory, UniqueID, Visible ``` --- ## Code Example The following example demonstrates how to declare an `ImageButton` control and a `Label` control in an `.aspx` file. When the user clicks the image, the server-side click event handler is executed. This handler captures the exact X and Y coordinates of the click and displays them in the `Label` control. ### ASPX Page (`Default.aspx`) ```aspx <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="YourApp.Default" %> ImageButton Control Example

Click anywhere on the image below:



``` ### Code-Behind (`Default.aspx.cs`) ```csharp using System; using System.Web.UI; namespace YourApp { public partial class Default : Page { protected void Page_Load(object sender, EventArgs e) { } // The click event handler for ImageButton uses ImageClickEventArgs // to retrieve the X and Y coordinates of the click. protected void imgButton_Click(object sender, ImageClickEventArgs e) { lblMessage.Text = $"Coordinates: X = {e.X}, Y = {e.Y}"; } } } ``` --- ## Key Considerations 1. **Image Coordinates (`ImageClickEventArgs`):** Unlike a standard `Button` click event which uses `EventArgs`, the `ImageButton` click event uses `ImageClickEventArgs`. This object contains the `X` and `Y` properties representing the pixel coordinates where the user clicked relative to the top-left corner of the image. 2. **Accessibility:** Always provide an `AlternateText` property. This ensures screen readers can describe the button's purpose to visually impaired users, and displays placeholder text if the image fails to load. 3. **Client-Side Validation:** If you use `OnClientClick` to run JavaScript before posting back, you can return `false` in your script to cancel the postback (e.g., `OnClientClick="return confirm('Are you sure?');"`).
← Control LabelControl Image β†’