HtmlInputImage.OnServerClick(ImageClickEventArgs) 方法

定义

引发 ServerClick 事件。

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)

参数

e
ImageClickEventArgs

包含事件数据的 ImageClickEventArgs

示例

下面的代码示例演示如何为 事件指定和编码处理程序 ServerClick ,以确定用户单击控件的 HtmlInputImage 坐标。

<%@ 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>

注解

ServerClick当用户单击控件HtmlInputImage时,将引发 该事件。

用户单击HtmlInputImage控件的坐标可以使用 对象的 和 ImageClickEventArgs.Y 属性System.Web.UI.ImageClickEventArgs来确定ImageClickEventArgs.X。 对象 System.Web.UI.ImageClickEventArgs 作为参数传递给用户定义的事件处理程序。

注意

原点 (0, 0) 位于图像的左上角。

引发事件时,将通过委托调用事件处理程序。 有关详细信息,请参阅 处理和引发事件

OnServerClick 方法还允许派生类对事件进行处理而不必附加委托。 这是在派生类中处理事件的首选技术。

继承者说明

在派生类中重写 OnServerClick(ImageClickEventArgs) 时,一定要调用基类的 OnServerClick(ImageClickEventArgs) 方法,以便已注册的委托对事件进行接收。

适用于

另请参阅