下面的代码示例演示如何使用 ButtonField 对象显示 GridView 控件中的一列命令按钮。
Visual Basic 复制代码 < %@ Page language = " VB " % > < script runat = " server " > Sub CustomersGridView_RowCommand() Sub CustomersGridView_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) ' 如果多個使用多個buttonfield,利用CommandName來確定那個buttonfield被點擊. If e.CommandName = "Select" Then '轉換列的索引放進CommandArgument Dim index As Integer = Convert.ToInt32(e.CommandArgument) ' 從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 ' 顯示所選作者. 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 = " <%___FCKpd___0nbsp;ConnectionStrings:NorthWindConnection%> " runat = " server " > </ asp:sqldatasource > </ form > </ body > </ html > C# <% @ 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 >