gridview 中的单击某一行中的某一控件怎么获得该行的行号
2013-04-26
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class Default10 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
IList<string> list = new List<string>();
list.Add("aa");
list.Add("bb");
list.Add("cc");
list.Add("dd");
list.Add("ee");
list.Add("ff");
list.Add("gg");
list.Add("hh");
GridView1.DataSource = list;
GridView1.DataBind();
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "rowindex")
{
int index=((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent)).RowIndex;
Label1.Text = index.ToString();
}
}
}
前台代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
操作的行号:<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" CommandName="rowindex" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>