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


データ バインド コントロール (GridView、DetailsView など) は、この ButtonField クラスを使用して、各レコードを表示するためのボタンを表示します。ButtonField オブジェクトは、そのオブジェクトを使用しているデータ バインド コントロールにより異なった形式で表示されます。たとえば、ButtonField オブジェクトは、GridView コントロールでは列として表示され、DetailsView コントロールでは行として表示されます。
ボタン フィールドのボタンをクリックすると、親データ バインド コントロールのコマンド イベントが発生します。コマンド イベントのイベント ハンドラを用意すると、コマンド ボタンがクリックされたときにカスタム ルーチンを実行できます。
![]() |
---|
GridView コントロールは RowCommand イベントを発生させます。一方、DetailsView コントロールは ItemCommand イベントを発生させます。 |
コマンド イベントを発生させたレコードのインデックスを特定するには、データ バインド コントロールのコマンド イベントに渡されるイベント引数の CommandArgument プロパティを使用します。ButtonField クラスは、適切なインデックス値を持つ CommandArgument プロパティを自動的に作成します。
表示するボタンの型を指定するには、ButtonType プロパティを使用します。リンクまたはコマンド ボタンを表示する場合、Text プロパティを使用してボタンに表示されるキャプションを指定します。
ButtonField オブジェクトをデータ ソースのフィールドにバインドすることでも同じ設定を行うことができます。この方法で設定すると、ButtonField オブジェクトの各ボタンで異なるキャプションを表示できます。ボタンのテキスト キャプションには、指定したフィールドの値が使用されます。ButtonField オブジェクトをデータ ソースのフィールドにバインドするには、DataTextField プロパティを設定します。
イメージ ボタンを表示する場合、ImageUrl プロパティを使用して、ButtonField オブジェクトのボタンに表示するイメージを指定します。
Visible プロパティを false に設定すると、データ バインド コントロールの ButtonField オブジェクトを非表示にできます。
ButtonField オブジェクトでは、ヘッダーおよびフッター セクションをカスタマイズできます。ヘッダーまたはフッター セクションのキャプションを表示するには、HeaderText または FooterText プロパティをそれぞれ設定します。HeaderImageUrl プロパティを設定すると、ヘッダー セクションにテキストを表示する代わりにイメージを表示できます。ButtonField オブジェクトのヘッダー セクションを非表示にするには、 ShowHeader プロパティを false に設定します。
![]() |
---|
一部のデータ バインド コントロール (GridView コントロールなど) では、コントロールのヘッダー セクション全体を表示または非表示にできます。これらのデータ バインド コントロールは、個々のボタン フィールドの ShowHeader プロパティをサポートしていません。データ バインド コントロールのヘッダー セクション全体を表示または非表示にするには、コントロールの ShowHeader プロパティを使用します (データ バインド コントロールがヘッダー セクション全体の表示または非表示に対応している場合のみ) 。 |
また、フィールドの各部分にスタイル プロパティを設定すると、ButtonField オブジェクトの外観 (フォントの色や背景色など) をカスタマイズできます。さまざまなスタイル プロパティの一覧を次の表に示します。

ButtonField オブジェクトを使用して、GridView コントロールにコマンド ボタンの列を表示する方法を次のコード例に示します。
<%@ Page language="VB" %> <script runat="server"> Sub CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) ' If multiple ButtonField column fields are used, use the ' CommandName property to determine which button was clicked. If e.CommandName = "Select" Then ' Convert the row index stored in the CommandArgument ' property to an Integer. Dim index As Integer = Convert.ToInt32(e.CommandArgument) ' Get the last name of the selected author from the appropriate ' cell in the GridView control. Dim selectedRow As GridViewRow = CustomersGridView.Rows(index) Dim contactCell As TableCell = selectedRow.Cells(1) Dim contact As String = contactCell.Text ' Display the selected author. Message.Text = "You selected " & contact & "." End If End Sub </script> <html> <body> <form id="Form1" runat="server"> <h3>ButtonField Example</h3> <asp:label id="Message" forecolor="Red" runat="server"/> <!-- Populate the Columns collection declaratively. --> <asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" autogeneratecolumns="false" onrowcommand="CustomersGridView_RowCommand" runat="server"> <columns> <asp:buttonfield buttontype="Button" commandname="Select" headertext="Select Customer" text="Select"/> <asp:boundfield datafield="CompanyName" headertext="Company Name"/> <asp:boundfield datafield="ContactName" headertext="Contact Name"/> </columns> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. --> <asp:sqldatasource id="CustomersSqlDataSource" selectcommand="Select [CustomerID], [CompanyName], [ContactName], [ContactTitle] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnection%>" runat="server"> </asp:sqldatasource> </form> </body> </html>
<%@ Page language="C#" %> <script runat="server"> void CustomersGridView_RowCommand(Object sender, GridViewCommandEventArgs e) { // If multiple ButtonField column fields are used, use the // CommandName property to determine which button was clicked. if(e.CommandName=="Select") { // Convert the row index stored in the CommandArgument // property to an Integer. int index = Convert.ToInt32(e.CommandArgument); // Get the last name of the selected author from the appropriate // cell in the GridView control. GridViewRow selectedRow = CustomersGridView.Rows[index]; TableCell contactName = selectedRow.Cells[1]; string contact = contactName.Text; // Display the selected author. Message.Text = "You selected " + contact + "."; } } </script> <html> <body> <form runat="server"> <h3>ButtonField Example</h3> <asp:label id="Message" forecolor="Red" runat="server"/> <!-- Populate the Columns collection declaratively. --> <asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" autogeneratecolumns="false" onrowcommand="CustomersGridView_RowCommand" runat="server"> <columns> <asp:buttonfield buttontype="Button" commandname="Select" headertext="Select Customer" text="Select"/> <asp:boundfield datafield="CompanyName" headertext="Company Name"/> <asp:boundfield datafield="ContactName" headertext="Contact Name"/> </columns> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. --> <asp:sqldatasource id="CustomersSqlDataSource" selectcommand="Select [CustomerID], [CompanyName], [ContactName], [ContactTitle] From [Customers]" connectionstring="<%$ ConnectionStrings:NorthWindConnection%>" runat="server"> </asp:sqldatasource> </form> </body> </html>


System.Web.UI.WebControls.DataControlField
System.Web.UI.WebControls.ButtonFieldBase
System.Web.UI.WebControls.ButtonField


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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


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