Button クラスとは? わかりやすく解説

Button クラス

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

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

Public Class Button
    Inherits WebControl
    Implements IButtonControl, IPostBackEventHandler
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設定します

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

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

<%@ Page Language="VB" AutoEventWireup="True"
 %>

<html>
<head>
   <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 runat="server">

      <h3>Button Example</h3>

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

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

<html>
<head>
   <script language="C#" runat=server>

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

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

      <h3>Button Example</h3>

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

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

<html>
<head>
   <script language="JScript" runat=server>

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

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

      <h3>Button Example</h3>

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

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

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

<%@ Page Language="VB" AutoEventWireup="True"
 %>

<html>
<head>

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

<html>
<head>

   <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 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 のセキュリティ.NET Frameworkセキュリティ
継承階層継承階層
System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
      System.Web.UI.WebControls.Button
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

Button クラス

Windows ボタン コントロール示します

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

<ComVisibleAttribute(True)> _
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
Public Class Button
    Inherits ButtonBase
    Implements IButtonControl
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] 
public class Button : ButtonBase, IButtonControl
[ComVisibleAttribute(true)] 
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] 
public ref class Button : public
 ButtonBase, IButtonControl
/** @attribute ComVisibleAttribute(true) */ 
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ 
public class Button extends ButtonBase implements
 IButtonControl
ComVisibleAttribute(true) 
ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) 
public class Button extends
 ButtonBase implements IButtonControl
解説解説
使用例使用例

Button作成し、その DialogResult プロパティを DialogResult.OK に設定してForm追加するコード例次に示します

Private Sub InitializeMyButton()
    ' Create and initialize a Button.
    Dim button1 As New Button()
    
    ' Set the button to return a value of OK when clicked.
    button1.DialogResult = DialogResult.OK
    
    ' Add the button to the form.
    Controls.Add(button1)
End Sub 'InitializeMyButton
private void InitializeMyButton()
 {
    // Create and initialize a Button.
    Button button1 = new Button();
 
    // Set the button to return a value of OK when clicked.
    button1.DialogResult = DialogResult.OK;
 
    // Add the button to the form.
    Controls.Add(button1);
 }
 
private:
   void InitializeMyButton()
   {
      // Create and initialize a Button.
      Button^ button1 = gcnew Button;
      
      // Set the button to return a value of OK when clicked.
      button1->DialogResult = ::DialogResult::OK;
      
      // Add the button to the form.
      Controls->Add( button1 );
   }
private void InitializeMyButton()
{
    // Create and initialize a Button.
    Button button1 = new Button();
    
    // Set the button to return a value of OK when clicked.
    button1.set_DialogResult(get_DialogResult().OK);
    
    // Add the button to the form.
    get_Controls().Add(button1);
} //InitializeMyButton
private function InitializeMyButton()
 {
    // Create and initialize a Button.
    var button1 : Button = new Button();
 
    // Set the button to return a value of OK when clicked.
    button1.DialogResult = System.Windows.Forms.DialogResult.OK;
 
    // Add the button to the form.
    Controls.Add(button1);
 }
 
継承階層継承階層
System.Object
   System.MarshalByRefObject
     System.ComponentModel.Component
       System.Windows.Forms.Control
         System.Windows.Forms.ButtonBase
          System.Windows.Forms.Button
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「Button クラス」の関連用語

Button クラスのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Button クラスのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS