HtmlInputImage.OnServerClick(ImageClickEventArgs) Method

Definition

Raises the ServerClick event.

protected:
 virtual void OnServerClick(System::Web::UI::ImageClickEventArgs ^ e);
protected virtual void OnServerClick (System.Web.UI.ImageClickEventArgs e);
abstract member OnServerClick : System.Web.UI.ImageClickEventArgs -> unit
override this.OnServerClick : System.Web.UI.ImageClickEventArgs -> unit
Protected Overridable Sub OnServerClick (e As ImageClickEventArgs)

Parameters

e
ImageClickEventArgs

An ImageClickEventArgs that contains event data.

Examples

The following code example demonstrates how to specify and code a handler for the ServerClick event to determine the coordinates where the user clicks the HtmlInputImage control.

<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
      
  protected void  ImageBtn_Click(object sender, ImageClickEventArgs e)
  {

    // Write the click coordinates to the Span1 element.
    Span1.InnerText = "You clicked at (" + e.X.ToString() +
                      ", " + e.Y.ToString() + ").";
  }
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server">
    <title>Click the Image </title>
</head>
<body>

   <form id="form1" runat="server">

      <h3>Click the Image </h3>

      <input type="image"
             alt="Image Alternate Text"
             src="Image1.jpg" 
             onserverclick="ImageBtn_Click"
             runat="server" id="Image1"/>

      <br />
      <br />

      <span id="Span1" 
            runat="server"/>

   </form>

</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
      
  Sub ImageBtn_Click(ByVal Sender As Object, ByVal E As ImageClickEventArgs)

    ' Write the click coordinates to the Span1 element.
    Span1.InnerText = "You clicked at (" & E.X.ToString() & _
                      ", " & E.Y.ToString() & ")."
      
  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server">
    <title>Click the Image </title>
</head>
<body>

   <form id="form1" runat="server">

      <h3>Click the Image </h3>

      <input type="image"
             alt="Image Alternate Text"
             src="Image1.jpg" 
             onserverclick="ImageBtn_Click"
             runat="server"/>

      <br />
      <br />

      <span id="Span1" 
            runat="server"/>

   </form>

</body>
</html>
<%@ Page Language="C#" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
      
  void ImageBtn_Click(Object sender, ImageClickEventArgs e)
  {

    // Display the coordinates of the position where the image 
    // was clicked. 
    Span1.InnerText = "You clicked at (" + e.X.ToString() +
                      ", " + e.Y.ToString() + ").";

  }

  void Page_Load(Object sender, EventArgs e)
  {

    // Create an EventHandler delegate for the method you want to 
    // handle the event, and then add it to the list of methods called
    // when the event is raised.
    Image1.ServerClick += new ImageClickEventHandler(this.ImageBtn_Click);

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HtmlInputImage ServerClick Example </title>
</head>
<body>

   <form id="form1" runat="server">

      <h3>HtmlInputImage ServerClick Example </h3>

      <input type="image"
             id="Image1"
             src="Image.jpg" 
             alt="Image"
             runat="server"/>

      <br />

      <span id="Span1" 
            runat="server"/>

   </form>

</body>
</html>
<%@ Page Language="VB" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
      
  Sub ImageBtn_Click(ByVal sender As Object, ByVal e As ImageClickEventArgs)

    ' Display the coordinates of the position where the image 
    ' was clicked.
    Span1.InnerText = "You clicked at (" & e.X.ToString() & _
                      ", " & e.Y.ToString() & ")."
      
  End Sub
  
  Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
      
    ' Create an EventHandler delegate for the method you want to 
    ' handle the event, and then add it to the list of methods called
    ' when the event is raised.
    AddHandler Image1.ServerClick, AddressOf ImageBtn_Click

  End Sub

</script>

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server">
    <title>HtmlInputImage ServerClick Example </title>
</head>
<body>

   <form id="form1" runat="server">

      <h3>HtmlInputImage ServerClick Example </h3>

      <input type="image"
             id="Image1"
             src="Image.jpg" 
             alt="Image"
             runat="server"/>

      <br />

      <span id="Span1" 
            runat="server"/>

   </form>

</body>
</html>

Remarks

The ServerClick event is raised when the user clicks an HtmlInputImage control.

The coordinates where the user clicks an HtmlInputImage control can be determined by using the ImageClickEventArgs.X and ImageClickEventArgs.Y properties of the System.Web.UI.ImageClickEventArgs object. The System.Web.UI.ImageClickEventArgs object is passed as a parameter to the user-defined event handler.

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 Handling and Raising Events.

The OnServerClick 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 OnServerClick(ImageClickEventArgs) in a derived class, be sure to call the base class's OnServerClick(ImageClickEventArgs) method so that registered delegates receive the event.

Applies to

See also