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


CommandField クラスは、データ バインド コントロール (GridView および DetailsView など) で使用される特殊なフィールドで、削除、編集、挿入、または選択の各操作を実行するコマンド ボタンを表示します。これらの操作を実行するコマンド ボタンは、次の表に示すプロパティを使用して表示または非表示にできます。
ShowDeleteButton | データ バインド コントロール内のレコードごとに、CommandField フィールドの Delete ボタンを表示または非表示にします。Delete ボタンを使用すると、ユーザーはデータ ソースからレコードを削除できます。 |
ShowEditButton | データ バインド コントロール内のレコードごとに、CommandField フィールドの Edit ボタンを表示または非表示にします。Edit ボタンを使用すると、ユーザーはデータ ソースのレコードを編集できます。ユーザーが特定のレコードの Edit ボタンをクリックすると、Edit ボタンは Update ボタンと Cancel ボタンに置き換わります。その他のコマンド ボタンもすべて非表示になります。 |
ShowInsertButton | CommandField フィールドの New ボタンを表示または非表示にします。New ボタンを使用すると、ユーザーはデータ ソースに新しいレコードを挿入できます。ユーザーが New ボタンをクリックすると、Insert ボタンと Cancel ボタンに置き換わります。その他のコマンド ボタンもすべて非表示になります。 |
ShowSelectButton | データ バインド コントロール内のレコードごとに、CommandField フィールドの Select ボタンを表示または非表示にします。Select ボタンを使用すると、ユーザーはデータ バインド コントロール内のレコードを選択できます。 |
また、ShowCancelButton プロパティを設定することにより、レコードが編集モードまたは挿入モードのときに表示される Cancel ボタンを表示または非表示にすることもできます。
![]() |
---|
データ バインド コントロールをデータ ソース コントロール (SqlDataSource コントロールなど) と組み合わせて使用すると、データ バインド コントロールはデータ ソース コントロールの機能を利用して、削除、更新、および挿入の自動実行機能を提供できます。その他のデータ ソースの場合、データ バインド コントロールの適切なイベント中にこれらの操作を実行するためにルーチンを提供する必要があります。 |
CommandField フィールドは、それが使用されているデータ バインド コントロールによって表示が異なります。たとえば、CommandField フィールドは、GridView コントロールでは列として表示され、DetailsView コントロールでは行として表示されます。
表示するボタンの種類を指定するには、ButtonType プロパティを使用します。ButtonType プロパティが ButtonType.Button または ButtonType.Link に設定されている場合、次の表に示すプロパティを設定して、ボタンに表示するテキストを指定できます。
CancelText | |
DeleteText | |
EditText | |
InsertText | |
NewText | |
SelectText | |
UpdateText |
コマンド ボタンまたはリンク ボタンを表示する代わりに、ButtonType プロパティを ButtonType.Image に設定し、次の表に示すプロパティを設定して、イメージ ボタンを表示できます。
CancelImageUrl | |
DeleteImageUrl | |
EditImageUrl | |
InsertText | |
NewImageUrl | |
SelectImageUrl | |
UpdateImageUrl |
既定では、ユーザーが CommandField フィールドでボタンをクリックすると、ページのすべての検証コントロールで検証が実行されます。ボタンをクリックしたときに検証が行われないようにするには、CausesValidation プロパティを false に設定します。
Visible プロパティを false に設定すると、データ バインド コントロールの CommandField フィールドを非表示にできます。
CommandField フィールドを使用すると、ヘッダー セクションおよびフッター セクションをカスタマイズできます。ヘッダー セクションまたはフッター セクションにキャプションを表示するには、HeaderText プロパティまたは FooterText プロパティをそれぞれ設定します。ヘッダー セクションにテキストを表示する代わりに、HeaderImageUrl プロパティを設定してイメージを表示できます。CommandField オブジェクトのヘッダー セクションを非表示にするには、ShowHeader プロパティを false に設定します。
![]() |
---|
一部のデータ バインド コントロール (GridView コントロールなど) では、コントロールのヘッダー セクション全体を表示または非表示にできます。これらのデータ バインド コントロールは、個別のボタン フィールドの ShowHeader プロパティをサポートしていません。データ バインド コントロールのヘッダー セクション (存在する場合) 全体を表示または非表示にするには、コントロールの ShowHeader プロパティを使用します。 |
フィールドの各部分にスタイル プロパティを設定することにより、CommandField オブジェクトの外観 (フォントの色や背景色など) をカスタマイズすることもできます。さまざまなスタイル プロパティの一覧を次の表に示します。

CommandField フィールドを使用してコントロールを表示し、GridView コントロール内のレコードを編集する方法を次のコード例に示します。
<%@ Page language="VB" %> <html> <body> <form runat="server"> <h3>CommandField Example</h3> <asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" autogeneratecolumns="false" datakeynames="CustomerID" runat="server"> <columns> <asp:commandfield showeditbutton="true" headertext="Edit Controls"/> <asp:boundfield datafield="CustomerID" headertext="Customer ID" /> <asp:boundfield datafield="CompanyName" headertext="Company Name"/> <asp:boundfield datafield="Address" headertext="Address"/> <asp:boundfield datafield="City" headertext="City"/> <asp:boundfield datafield="PostalCode" headertext="ZIP Code"/> <asp:boundfield datafield="Country" headertext="Country"/> </columns> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="CustomersSqlDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" updatecommand="Update Customers Set CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"> </asp:sqldatasource> </form> </body> </html>
<%@ Page language="C#" %> <html> <body> <form runat="server"> <h3>CommandField Example</h3> <asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" autogeneratecolumns="false" datakeynames="CustomerID" runat="server"> <columns> <asp:commandfield showeditbutton="true" headertext="Edit Controls"/> <asp:boundfield datafield="CustomerID" headertext="Customer ID" /> <asp:boundfield datafield="CompanyName" headertext="Company Name"/> <asp:boundfield datafield="Address" headertext="Address"/> <asp:boundfield datafield="City" headertext="City"/> <asp:boundfield datafield="PostalCode" headertext="ZIP Code"/> <asp:boundfield datafield="Country" headertext="Country"/> </columns> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="CustomersSqlDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" updatecommand="Update Customers Set CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"> </asp:sqldatasource> </form> </body> </html>
CancelText、EditText、および UpdateText の各プロパティを使用して、編集コントロールのテキストをカスタマイズする方法を次のコード例に示します。
<%@ Page language="VB" %> <html> <body> <form runat="server"> <h3>CommandField Example</h3> <asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" autogeneratecolumns="false" datakeynames="CustomerID" runat="server"> <columns> <asp:commandfield showeditbutton="true" edittext="Edit Customer" canceltext="Discard" updatetext="Revise" headertext="Edit Controls"/> <asp:boundfield datafield="CustomerID" headertext="Customer ID" /> <asp:boundfield datafield="CompanyName" headertext="Company Name"/> <asp:boundfield datafield="Address" headertext="Address"/> <asp:boundfield datafield="City" headertext="City"/> <asp:boundfield datafield="PostalCode" headertext="ZIP Code"/> <asp:boundfield datafield="Country" headertext="Country"/> </columns> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="CustomersSqlDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" updatecommand="Update Customers Set CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"> </asp:sqldatasource> </form> </body> </html>
<%@ Page language="C#" %> <html> <body> <form runat="server"> <h3>CommandField Example</h3> <asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" autogeneratecolumns="false" datakeynames="CustomerID" runat="server"> <columns> <asp:commandfield showeditbutton="true" edittext="Edit Customer" canceltext="Discard" updatetext="Revise" headertext="Edit Controls"/> <asp:boundfield datafield="CustomerID" headertext="Customer ID" /> <asp:boundfield datafield="CompanyName" headertext="Company Name"/> <asp:boundfield datafield="Address" headertext="Address"/> <asp:boundfield datafield="City" headertext="City"/> <asp:boundfield datafield="PostalCode" headertext="ZIP Code"/> <asp:boundfield datafield="Country" headertext="Country"/> </columns> </asp:gridview> <!-- This example uses Microsoft SQL Server and connects --> <!-- to the Northwind sample database. Use an ASP.NET --> <!-- expression to retrieve the connection string value --> <!-- from the Web.config file. --> <asp:sqldatasource id="CustomersSqlDataSource" selectcommand="Select [CustomerID], [CompanyName], [Address], [City], [PostalCode], [Country] From [Customers]" updatecommand="Update Customers Set CompanyName=@CompanyName, Address=@Address, City=@City, PostalCode=@PostalCode, Country=@Country Where (CustomerID = @CustomerID)" connectionstring="<%$ ConnectionStrings:NorthWindConnectionString%>" runat="server"> </asp:sqldatasource> </form> </body> </html>


System.Web.UI.WebControls.DataControlField
System.Web.UI.WebControls.ButtonFieldBase
System.Web.UI.WebControls.CommandField


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


CommandField メンバ
System.Web.UI.WebControls 名前空間
GridView
Columns
DetailsView
Fields
BoundField クラス
ButtonField クラス
ButtonFieldBase クラス
CheckBoxField クラス
DataControlField
HyperLinkField
TemplateField
ButtonFieldBase.ButtonType プロパティ
CancelText
DeleteImageUrl
DeleteText
EditImageUrl
EditText
InsertImageUrl
InsertText
NewImageUrl
NewText
SelectImageUrl
SelectText
ShowCancelButton
ShowDeleteButton
ShowEditButton
ShowInsertButton
ShowSelectButton
UpdateImageUrl
UpdateText
ControlStyle
FooterStyle
HeaderStyle
ItemStyle
- CommandField クラスのページへのリンク