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


ButtonField クラスの新しいインスタンスを初期化するには、ButtonField コンストラクタを使用します。通常、このコンストラクタは、動的に作成されたデータ バインド コントロールにフィールドを追加する場合に使用されます。
ButtonField オブジェクトをデータ バインド コントロールに動的に追加するには、新しい ButtonField オブジェクトを作成して、プロパティを設定してから、そのオブジェクトをデータ バインド コントロールのフィールド コレクションに追加します。たとえば、GridView コントロールを使用する場合は、ButtonField オブジェクトを Columns コレクションに追加します。
![]() |
---|
データ バインド コントロールには動的にフィールドを追加できますが、フィールドは静的に宣言しておき、適宜、表示または非表示にすることをお勧めします。フィールドをすべて静的に宣言すると、親データ バインド コントロールのビューステートのサイズを削減できます。 |

M:System.Web.UI.WebControls.ButtonField.#ctor コンストラクタを使用して、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 Customer from the appropriate ' cell in the GridView control. Dim selectedRow As GridViewRow = CustomersGridView.Rows(index) Dim contactNameCell As TableCell = selectedRow.Cells(1) Dim contactName As String = contactNameCell.Text ' Display the selected Customer. Message.Text = "You selected " & contactName & "." End If End Sub Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) ' The field columns need to be created only the first time ' the page is loaded. If Not IsPostBack Then ' Dynamically create field columns to display the desired ' fields from the data source. ' Create a ButtonField object to allow the user to ' select an Customer. Dim selectButtonField As New ButtonField selectButtonField.ButtonType = ButtonType.Button selectButtonField.CommandName = "Select" selectButtonField.HeaderText = "Select Customer" selectButtonField.Text = "Select" ' Create a BoundField object to display an Customer's last name. Dim contactNameBoundField As New BoundField contactNameBoundField.DataField = "ContactName" contactNameBoundField.HeaderText = "Contact Name" ' Create a BoundField object to display an Customer's first name. Dim contactTitleBoundField As New BoundField contactTitleBoundField.DataField = "ContactTitle" contactTitleBoundField.HeaderText = "Contact Title" ' Add the field columns to the Columns collection of the ' GridView control. CustomersGridView.Columns.Add(selectButtonField) CustomersGridView.Columns.Add(contactNameBoundField) CustomersGridView.Columns.Add(contactTitleBoundField) End If End Sub </script> <html> <body> <form id="Form1" runat="server"> <h3>ButtonField Constructor Example</h3> <asp:label id="Message" forecolor="Red" runat="server"/> <asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" autogeneratecolumns="False" onrowcommand="CustomersGridView_RowCommand" runat="server"> </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 Customer from the appropriate // cell in the GridView control. GridViewRow selectedRow = CustomersGridView.Rows[index]; TableCell contactCell = selectedRow.Cells[1]; string contact = contactCell.Text; // Display the selected Customer. Message.Text = "You selected " + contact + "."; } } void Page_Load(Object sender, EventArgs e) { // The field columns need to be created only the first time // the page is loaded. if (!IsPostBack) { // Dynamically create field columns to display the desired // fields from the data source. // Create a ButtonField object to allow the user to // select an Customer. ButtonField selectButtonField = new ButtonField (); selectButtonField.ButtonType = ButtonType.Button; selectButtonField.CommandName = "Select"; selectButtonField.HeaderText = "Select Customer"; selectButtonField.Text = "Select"; // Create a BoundField object to display an Customer's last name. BoundField contactNameBoundField = new BoundField(); contactNameBoundField.DataField = "ContactName"; contactNameBoundField.HeaderText = "Contact Name"; // Create a BoundField object to display an Customer's first name. BoundField contactTitleBoundField = new BoundField(); contactTitleBoundField.DataField = "ContactTitle"; contactTitleBoundField.HeaderText = "Contact Title"; // Add the field columns to the Columns collection of the // GridView control. CustomersGridView.Columns.Add (selectButtonField); CustomersGridView.Columns.Add(contactNameBoundField); CustomersGridView.Columns.Add(contactTitleBoundField); } } </script> <html> <body> <form runat="server"> <h3>ButtonField Constructor Example</h3> <asp:label id="Message" forecolor="Red" runat="server"/> <asp:gridview id="CustomersGridView" datasourceid="CustomersSqlDataSource" autogeneratecolumns="False" onrowcommand="CustomersGridView_RowCommand" runat="server"> </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>

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 コンストラクタのページへのリンク