Obtain the index after the DridView button in ASP. NET

Source: Internet
Author: User

 

<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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.