Button クラス

Web ページにプッシュ ボタン コントロールを表示します。

名前空間: System.Web.UI.WebControls
アセンブリ: System.Web (system.web.dll 内)

構文

'宣言
Public Class Button
    Inherits WebControl
    Implements IButtonControl, IPostBackEventHandler
'使用
Dim instance As Button
public class Button : WebControl, IButtonControl, IPostBackEventHandler
public ref class Button : public WebControl, IButtonControl, IPostBackEventHandler
public class Button extends WebControl implements IButtonControl, IPostBackEventHandler
public class Button extends WebControl implements IButtonControl, IPostBackEventHandler
適用できません。

解説

Button コントロールを使用して、Web ページにプッシュ ボタンを作成します。送信ボタンまたはコマンド ボタンを作成できます。

既定では、Button コントロールは送信ボタンです。送信ボタンに関連付けられているコマンド名 (CommandName プロパティで指定) はありません。このボタンは Web ページをサーバーにポストバックするだけです。Click イベントのイベント ハンドラを作成して、送信ボタンがクリックされたときに実行されるアクションをプログラムにより制御できます。

コマンド ボタンの場合は、CommandName プロパティを設定することにより、Sort などのコマンド名を関連付けることができます。コマンド名を設定すると、Web ページに複数の Button コントロールを作成し、どの Button コントロールがクリックされたかをプログラムによって確認できます。コマンド ボタンの場合は、CommandArgument プロパティを使用して、実行するコマンドに関して昇順などの追加情報も指定できます。Command イベントのイベント ハンドラを作成して、コマンド ボタンがクリックされたときに実行されるアクションをプログラムによって制御できます。

既定では、Button コントロールがクリックされたときにページ検証を実行します。ページ検証は、ページ上にある検証コントロールに関連付けられたすべての入力コントロールが、その検証コントロールによって指定されている検証規則に準拠しているかどうかを判断します。ページ検証を実行しないようにするには、CausesValidation プロパティを false に設定します。

ユーザー補助

このコントロールに既定でレンダリングされるマークアップは、Web Content Accessibility Guidelines (WCAG) 1.0 の優先度 1 ガイドラインなどのユーザー補助に関する標準に適合しない可能性があります。このコントロールのユーザー補助サポートの詳細については、「ASP.NET コントロールとユーザー補助」を参照してください。

トピック 場所
チュートリアル : ASP.NET Web サーバー コントロール用の基本的なコントロール デザイナの作成 ASP.NET コントロールの作成
チュートリアル : ASP.NET ユーザー コントロールによる再利用可能な要素の作成 Visual Studio での ASP .NET Web アプリケーションの作成
方法 : ASP.NET Web ページでイベント ハンドラを作成する (Visual Studio) Visual Studio での ASP .NET Web アプリケーションの作成
チュートリアル : カスタム ビジネス オブジェクトへのデータ バインディング Visual Studio での ASP .NET Web アプリケーションの作成
チュートリアル: Visual Web Developer でのカスケード スタイル シートのスタイルの操作 Visual Studio での ASP .NET Web アプリケーションの作成
チュートリアル : Web フォーム ページにおけるユーザーの入力の検証 Visual Studio での ASP .NET Web アプリケーションの作成
方法 : Web フォーム ページに Button Web サーバー コントロールを追加する (Visual Studio) Visual Studio での ASP .NET Web アプリケーションの作成
チュートリアル : Visual Web Developer での ASP.NET マスタ ページの作成と使用 Visual Studio での ASP .NET Web アプリケーションの作成
チュートリアル : Visual Web Developer での基本的な Web ページの作成 Visual Studio での ASP .NET Web アプリケーションの作成
チュートリアル : Visual Studio でのテーマを使用した Web サイトのカスタマイズ Visual Studio での ASP .NET Web アプリケーションの作成
チュートリアル : Visual Web Developer での ASP.NET マスタ ページの作成と使用 Visual Web Developer でのアプリケーションの作成
チュートリアル : Web フォーム ページにおけるユーザーの入力の検証 Visual Web Developer でのアプリケーションの作成
チュートリアル : ASP.NET ユーザー コントロールによる再利用可能な要素の作成 Visual Web Developer でのアプリケーションの作成
チュートリアル : Visual Studio でのテーマを使用した Web サイトのカスタマイズ Visual Web Developer でのアプリケーションの作成
方法 : クライアント スクリプトで Button Web サーバー コントロール イベントに応答する Visual Studio ASP .NET での Web アプリケーションの作成
チュートリアル : Web サーバー コントロール用の基本的なコントロール デザイナの作成 Visual Studio ASP .NET での Web アプリケーションの作成
方法 : ASP.NET Web ページの 1 つのイベント ハンドラに複数のイベントを関連付ける Visual Studio ASP .NET での Web アプリケーションの作成
方法 : イベントを生成した Web サーバー コントロールを特定する Visual Studio ASP .NET での Web アプリケーションの作成
方法 : ASP.NET Web サーバー コントロールにフォーカスを設定する Visual Studio ASP .NET での Web アプリケーションの作成
方法 : ASP.NET Web ページでイベント ハンドラを作成する (Visual Studio) Visual Studio ASP .NET での Web アプリケーションの作成
チュートリアル : カスタム ビジネス オブジェクトへのデータ バインディング
 Visual Studio ASP .NET での Web アプリケーションの作成
チュートリアル : Visual Web Developer での基本的な Web ページの作成 Visual Studio ASP .NET での Web アプリケーションの作成
方法 : Button Web サーバー コントロール イベントに応答する Visual Studio ASP .NET での Web アプリケーションの作成
方法 : データ バインド コントロールのボタン イベントに応答する Visual Studio ASP .NET での Web アプリケーションの作成
方法 : DataList 項目または Repeater 項目のボタン イベントに応答する Visual Studio ASP .NET での Web アプリケーションの作成
方法 : クライアント スクリプトで Button Web サーバー コントロール イベントに応答する Visual Studio ASP .NET での Web アプリケーションの作成
チュートリアル : Web サーバー コントロール用の基本的なコントロール デザイナの作成 Visual Studio ASP .NET での Web アプリケーションの作成
方法 : ASP.NET Web ページの 1 つのイベント ハンドラに複数のイベントを関連付ける Visual Studio ASP .NET での Web アプリケーションの作成
方法 : イベントを生成した Web サーバー コントロールを特定する Visual Studio ASP .NET での Web アプリケーションの作成
方法 : ASP.NET Web サーバー コントロールにフォーカスを設定する Visual Studio ASP .NET での Web アプリケーションの作成
方法 : ASP.NET Web ページでイベント ハンドラを作成する (Visual Studio) Visual Studio ASP .NET での Web アプリケーションの作成
チュートリアル : カスタム ビジネス オブジェクトへのデータ バインディング
 Visual Studio ASP .NET での Web アプリケーションの作成
チュートリアル : Visual Web Developer での基本的な Web ページの作成 Visual Studio ASP .NET での Web アプリケーションの作成
方法 : Web フォーム ページに Button Web サーバー コントロールを追加する (Visual Studio) Visual Studio ASP .NET での Web アプリケーションの作成
方法 : Button Web サーバー コントロール イベントに応答する Visual Studio ASP .NET での Web アプリケーションの作成
方法 : データ バインド コントロールのボタン イベントに応答する Visual Studio ASP .NET での Web アプリケーションの作成
方法 : DataList 項目または Repeater 項目のボタン イベントに応答する Visual Studio ASP .NET での Web アプリケーションの作成
方法 : Web フォーム ページに Button Web サーバー コントロールを追加する (Visual Studio) Visual Studio ASP .NET での Web アプリケーションの作成
方法 : クライアント スクリプトで Button Web サーバー コントロール イベントに応答する ASP .NET Web アプリケーションの作成
方法 : ASP.NET Web ページの 1 つのイベント ハンドラに複数のイベントを関連付ける ASP .NET Web アプリケーションの作成
方法 : イベントを生成した Web サーバー コントロールを特定する ASP .NET Web アプリケーションの作成
方法 : Web フォーム ページに Button Web サーバー コントロールを追加する ASP .NET Web アプリケーションの作成
方法 : ASP.NET Web サーバー コントロールにフォーカスを設定する ASP .NET Web アプリケーションの作成
方法 : Web フォーム ページに PlaceHolder Web サーバー コントロールを追加する ASP .NET Web アプリケーションの作成
方法 : Button Web サーバー コントロール イベントに応答する ASP .NET Web アプリケーションの作成
方法 : データ バインド コントロールのボタン イベントに応答する ASP .NET Web アプリケーションの作成
方法 : DataList 項目、Repeater 項目、または GridView 項目のボタン イベントに応答する ASP .NET Web アプリケーションの作成

使用例

Web ページの内容をサーバーにポストバックする、送信 Button コントロールを作成する方法を次のコード例に示します。

<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Button Example</title>
<script language="VB" runat="server">
    Sub SubmitBtn_Click(sender As Object, e As EventArgs)
        Message.Text = "Hello World!!"
    End Sub 'SubmitBtn_Click
  </script>
</head>
<body>
   <form id="form1" runat="server">

      <h3>Button Example</h3>

      Click on the submit button.<br /><br />
 
      <asp:Button id="Button1"
           Text="Submit"
           OnClick="SubmitBtn_Click" 
           runat="server"/>
       
      <br />

      <asp:label id="Message" 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Button Example</title>
<script language="C#" runat="server">

      void SubmitBtn_Click(Object sender, EventArgs e) 
      {
         Message.Text="Hello World!!";
      }

   </script>
</head>
<body>
   <form id="form1" runat="server">

      <h3>Button Example</h3>

      Click on the submit button.<br /><br />
 
      <asp:Button id="Button1"
           Text="Submit"
           OnClick="SubmitBtn_Click" 
           runat="server"/>
       
      <br />

      <asp:label id="Message" runat="server"/>
 
   </form>
</body>
</html>
<%@ Page Language="JScript" AutoEventWireup="True" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Button Example</title>
<script language="JScript" runat="server">

      function SubmitBtn_Click(sender : Object, e : EventArgs) 
      {
         Message.Text="Hello World!!";
      }

   </script>
</head>
<body>
   <form id="form1" runat="server">

      <h3>Button Example</h3>

      Click on the submit button.<br /><br />
 
      <asp:Button id="Button1"
           Text="Submit"
           OnClick="SubmitBtn_Click" 
           runat="server"/>
       
      <br />

      <asp:label id="Message" runat="server"/>
 
   </form>
</body>
</html>

リストを並べ替えるコマンド Button コントロールを作成する方法を次のコード例に示します。

<%@ 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Button CommandName Example</title>
<script runat="server">

      Sub CommandBtn_Click(sender As Object, e As CommandEventArgs) 

         Select e.CommandName

            Case "Sort"

               ' Call the method to sort the list.
               Sort_List(CType(e.CommandArgument, String))

            Case "Submit"

               ' Display a message for the Submit button being clicked.
               Message.Text = "You clicked the Submit button"

               ' Test whether the command argument is an empty string ("").
               If CType(e.CommandArgument , String) = "" Then
              
                  ' End the message.
                  Message.Text &= "."
               
               Else
               
                  ' Display an error message for the command argument. 
                  Message.Text &= ", however the command argument is not recogized."
               
               End If                

            Case Else

               ' The command name is not recognized. Display an error message.
               Message.Text = "Command name not recogized."

         End Select

      End Sub

      Sub Sort_List(commandArgument As String)

         Select commandArgument

            Case "Ascending"
 
               ' Insert code to sort the list in ascending order here.
               Message.Text = "You clicked the Sort Ascending button."

            Case "Descending"
              
               ' Insert code to sort the list in descending order here.
               Message.Text = "You clicked the Sort Descending button."

            Case Else
        
               ' The command argument is not recognized. Display an error message.
               Message.Text = "Command argument not recogized."

         End Select

      End Sub

   </script>

</head>
 
<body>

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

      <h3>Button CommandName Example</h3>

      Click on one of the command buttons.

      <br /><br />
 
      <asp:Button id="Button1"
           Text="Sort Ascending"
           CommandName="Sort"
           CommandArgument="Ascending"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button2"
           Text="Sort Descending"
           CommandName="Sort"
           CommandArgument="Descending"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      <br /><br />

      <asp:Button id="Button3"
           Text="Submit"
           CommandName="Submit"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button4"
           Text="Unknown Command Name"
           CommandName="UnknownName"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button5"
           Text="Submit Unknown Command Argument"
           CommandName="Submit"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click" 
           runat="server"/>
       
      <br /><br />

      <asp:Label id="Message" 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">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Button CommandName Example</title>
<script runat="server">

      void CommandBtn_Click(Object sender, CommandEventArgs e) 
      {

         switch(e.CommandName)
         {

            case "Sort":

               // Call the method to sort the list.
               Sort_List((String)e.CommandArgument);
               break;

            case "Submit":

               // Display a message for the Submit button being clicked.
               Message.Text = "You clicked the Submit button";

               // Test whether the command argument is an empty string ("").
               if((String)e.CommandArgument == "")
               {
                  // End the message.
                  Message.Text += ".";
               }
               else
               {
                  // Display an error message for the command argument. 
                  Message.Text += ", however the command argument is not recogized.";
               }                
               break;

            default:

               // The command name is not recognized. Display an error message.
               Message.Text = "Command name not recogized.";
               break; 

         }

      }

      void Sort_List(string commandArgument)
      {

         switch(commandArgument)
         {

            case "Ascending":
 
               // Insert code to sort the list in ascending order here.
               Message.Text = "You clicked the Sort Ascending button.";
               break;

            case "Descending":
              
               // Insert code to sort the list in descending order here.
               Message.Text = "You clicked the Sort Descending button.";
               break;

            default:
        
               // The command argument is not recognized. Display an error message.
               Message.Text = "Command argument not recogized.";
               break;

         }

      }

   </script>

</head>
 
<body>

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

      <h3>Button CommandName Example</h3>

      Click on one of the command buttons.

      <br /><br />
 
      <asp:Button id="Button1"
           Text="Sort Ascending"
           CommandName="Sort"
           CommandArgument="Ascending"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button2"
           Text="Sort Descending"
           CommandName="Sort"
           CommandArgument="Descending"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      <br /><br />

      <asp:Button id="Button3"
           Text="Submit"
           CommandName="Submit"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button4"
           Text="Unknown Command Name"
           CommandName="UnknownName"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click" 
           runat="server"/>

      &nbsp;

      <asp:Button id="Button5"
           Text="Submit Unknown Command Argument"
           CommandName="Submit"
           CommandArgument="UnknownArgument"
           OnCommand="CommandBtn_Click" 
           runat="server"/>
       
      <br /><br />

      <asp:Label id="Message" runat="server"/>
 
   </form>
 
</body>
</html>

.NET Framework のセキュリティ

  • AspNetHostingPermission  ホスト環境での動作に必要なアクセス許可。要求値 : LinkDemand; アクセス許可値 : Minimal
  • AspNetHostingPermission  ホスト環境での動作に必要なアクセス許可。要求値 : InheritanceDemand; アクセス許可値 : Minimal

継承階層

System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
      System.Web.UI.WebControls.Button

スレッド セーフ

この型の public static (Visual Basicでは共有) メンバはすべて,スレッド セーフです。インスタンス メンバの場合は,スレッド セーフであるとは限りません。

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

Button メンバ
System.Web.UI.WebControls 名前空間
CommandName
CommandArgument
Click
Command

その他の技術情報

Button Web サーバー コントロール