Export (0) Print
Expand All

ImageButton.OnClick Method

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

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

Parameters

e
A System.Web.UI.ImageClickEventArgs that contains the event data.

Remarks

The Click event is 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 these coordinates. Note the origin (0, 0) is located at the upper left corner of the image.

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

The OnClick 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 OnClick in a derived class, be sure to call the base class's OnClick 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 Click event to display the coordinates where the user clicks the image.

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

   <script language="VB" 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 language="C#" runat="server">

      void ImageButton_Click(object sender, 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" 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>


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

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

      function ImageButton_Click(sender : Object, e : ImageClickEventArgs) {
         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" 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++] 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 | Click | System.Web.UI.ImageClickEventArgs

Show:
© 2015 Microsoft