ImageButton クラス

イメージを表示し、そのイメージがマウス クリックされると応答するコントロール。

この型のすべてのメンバの一覧については、ImageButton メンバ を参照してください。

System.Object
   System.Web.UI.Control
      System.Web.UI.WebControls.WebControl
         System.Web.UI.WebControls.Image
            System.Web.UI.WebControls.ImageButton

Public Class ImageButton
   Inherits Image
   Implements IPostBackDataHandler, IPostBackEventHandler
[C#]
public class ImageButton : Image, IPostBackDataHandler,
   IPostBackEventHandler
[C++]
public __gc class ImageButton : public Image, IPostBackDataHandler,
   IPostBackEventHandler
[JScript]
public class ImageButton extends Image implements
   IPostBackDataHandler, IPostBackEventHandler

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

ImageButton コントロールを使用して、マウス クリックに応答するイメージを表示します。

ImageButton コントロールがクリックされると、 Click イベントと Command イベントの両方が発生します。

OnClick イベント ハンドラを使用することにより、イメージがクリックされた位置の座標をプログラムによって確認できます。その後、座標の値に基づいて、応答をコード化できます。原点 (0, 0) は、イメージの左上隅です。

OnCommand イベント ハンドラを使用して、 ImageButton コントロールがコマンド ボタンのような動作をするように設定できます。 CommandName プロパティを使用して、コマンド名をコントロールに関連付けることができます。これにより、複数の ImageButton コントロールを同一の Web ページ上に配置できます。さらに、 CommandName プロパティの値を OnCommand イベント ハンドラでプログラムにより識別して、各 ImageButton コントロールがクリックされたときに実行する適切なアクションを決定できます。 CommandArgument プロパティを使用して、昇順の指定など、コマンドについての追加情報を渡すこともできます。

使用例

[Visual Basic, C#, JScript] イメージがクリックされた位置の座標を表示する ImageButton コントロールを作成する方法の例を次に示します。

 
<%@ 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++] C++ のサンプルはありません。Visual Basic、C#、および JScript のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.Web.UI.WebControls

プラットフォーム: Windows 2000, Windows XP Professional, Windows Server 2003 ファミリ

アセンブリ: System.Web (System.Web.dll 内)

参照

ImageButton メンバ | System.Web.UI.WebControls 名前空間 | Button | Image