asp.net gridview click row
时间: 2023-09-09 21:12:08 浏览: 92
GridView的点击事件
4星 · 用户满意度95%
As an AI language model, I cannot click on a row in an ASP.NET GridView. However, I can provide you with the code example to handle the click event of a row in an ASP.NET GridView.
To handle the click event of a row in an ASP.NET GridView, you can use the RowCommand event of the GridView control. Here's an example:
1. Add a GridView control to your web form:
```html
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:BoundField DataField="Id" HeaderText="ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:ButtonField ButtonType="Button" Text="Edit" CommandName="EditRow" />
</Columns>
</asp:GridView>
```
2. Handle the RowCommand event of the GridView control:
```csharp
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "EditRow")
{
// Get the index of the row that was clicked
int rowIndex = Convert.ToInt32(e.CommandArgument);
// Get the value of the ID field of the selected row
string id = GridView1.DataKeys[rowIndex].Value.ToString();
// Do something with the ID, like redirect to an edit page
Response.Redirect("EditPage.aspx?id=" + id);
}
}
```
In this example, we handle the RowCommand event of the GridView control and check if the command name is "EditRow", which corresponds to the button field in the GridView. We then get the index of the row that was clicked and the value of the ID field of the selected row using the DataKeys collection. Finally, we do something with the ID, like redirect to an edit page.
阅读全文