protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Select") { int rowIndex = ((GridViewRow)((LinkButton)e.CommandSource).NamingContainer).RowIndex; Label1.Text = GridView1.DataKeys[rowIndex].Value.ToString(); } }
或
1 protected void LinkButton1_Command(object sender, CommandEventArgs e)2 {3 LinkButton lb = (LinkButton)sender;4 DataControlFieldCell dcf = (DataControlFieldCell)lb.Parent;5 GridViewRow gvr = (GridViewRow)dcf.Parent;6 GridView1.SelectedIndex = gvr.RowIndex;7 }