Button クラス
アセンブリ: System.Web (system.web.dll 内)


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 コントロールとユーザー補助」を参照してください。

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"/> <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"/> <asp:Button id="Button4" Text="Unknown Command Name" CommandName="UnknownName" CommandArgument="UnknownArgument" OnCommand="CommandBtn_Click" runat="server"/> <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"/> <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"/> <asp:Button id="Button4" Text="Unknown Command Name" CommandName="UnknownName" CommandArgument="UnknownArgument" OnCommand="CommandBtn_Click" runat="server"/> <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>


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


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Button クラス
アセンブリ: 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

Button は、マウスを使用してクリックできます。ボタンにフォーカスがある場合は Enter キーまたは Space キーを使用することもできます。
Form の AcceptButton プロパティまたは CancelButton プロパティを設定すると、ボタンにフォーカスがなくても Enter キーまたは Esc キーを押してボタンをクリックできます。この設定により、フォームがダイアログ ボックスとして動作します。
ShowDialog メソッドを使用してフォームを表示する場合は、ボタンの DialogResult プロパティを使用して ShowDialog の戻り値を指定します。
ボタンの外観は変更できます。たとえば、Web 用にフラットな外観にするには FlatStyle プロパティを FlatStyle.Flat に設定します。FlatStyle プロパティを FlatStyle.Popup に設定すると、マウス ポインタがボタンの上を通過するまではフラットで、通過した後は標準 Windows ボタンの外観となります。
![]() |
---|
フォーカスがあるコントロールが Enter キーによる入力を受け入れて処理する場合は、Button による処理は行われません。たとえば、複数行 TextBox またはほかのボタンにフォーカスがある場合、コントロールはボタン操作を受け入れずに Enter キーによる入力を処理します。 |

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 ); }

System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.ButtonBase
System.Windows.Forms.Button


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- Button クラスのページへのリンク