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(); ...... } }