<Span style = "font-family: 'Microsoft yahei'; font-size: 13px;"> <% @ Page language = "C #" %>
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN"
Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd>
<Script runat = "server">
Void CustomersGridView_RowCommand (Object sender, GridViewCommandEventArgs e)
{
// If multiple ButtonField column fields are used, use
// CommandName property to determine which button was clicked.
If (e. CommandName = "Select ")
{
// Convert the row index stored in the CommandArgument
// Property to an Integer.
/*
To determine the record index of the command event, use the CommandArgument attribute of the event parameter, which is passed to the command event of the data binding control. The ButtonField class automatically fills the CommandArgument attribute with the appropriate index value.
*/
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 xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> ButtonField Example </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<H3> ButtonField Example
<Asp: label id = "Message"
Forecolor = "Red"
Runat = "server"
AssociatedControlID = "CustomersGridView"/>
<! -- Populate the Columns collection declaratively. -->
<Asp: gridview id = "CustomersGridView"
Performanceid = "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>
</Span>
From hi_dzj's column