单击Gridview中LinkButton,获取当前行索引及某单元格值
1、在Gridview中添加模板列,在其中加入Linkbuttion,增加CommandName属性(设置命令名),并赋值
<asp:TemplateField HeaderText="记录编号">
<ItemTemplate>
<asp:LinkButton ID="lbtnRecordid" CommandName="lbtn" runat="server" ForeColor="Blue" Text='<%# DataBinder.Eval(Container.DataItem,"recordid") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
2、在Gridview的RowCommand事件中加入代码,例如:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "lbtn")
{
GridViewRow gvrow = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); //获取被点击的linkButton所在的GridViewRow
int index = gvrow.RowIndex; //获取到行索引 RowIndex
//获取当前行的某列值
string userid=GridView1.Rows[index].Cells[列索引].Text.Trim();
......
}
}