现在的位置: 首页 > 综合 > 正文

(转)在GridView中使用模板列LinkButton,如何知道当前的LinkButton点击在哪行记录上?

2014年01月07日 ⁄ 综合 ⁄ 共 1191字 ⁄ 字号 评论关闭
<asp:TemplateField HeaderText="Add new"> <ItemTemplate>

<asp:LinkButton ID="link1" runat="server" OnClick="lnk1Click"/>

</ItemTemplate> </asp:TemplateField>

 

<asp:TemplateField> <ItemTemplate>

 

<asp:LinkButton ID="link2" runat="server" OnClick="lnk2Click"/> 

 

</ItemTemplate></asp:TemplateField>

 

<asp:BoundField DataField="NAME" / >

 

<asp:BoundField DataField="DESC"/>

 

<asp:BoundField DataField="DESC2" />  

 

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
      GridViewRow gvr = e.Row; 
      switch (gvr.RowType)
      {
  case DataControlRowType.DataRow:
  {
       // Retrieve controls
       LinkButton link1 = gvr.FindControl("link1") as LinkButton;
       LinkButton link2 = gvr.FindControl("link2") as LinkButton;
       if (link1 != null)
       {
    link1.CommandArgument = gvr.RowIndex.ToString();
       }
             if (link2 != null)
       {
    link2.CommandArgument = gvr.RowIndex.ToString();
       }
       break;
  }
  }
}
protected void link1Click(object sender, EventArgs e)
{
    // Retrieve control
    LinkButton link1 = sender as LinkButton;
    int rowIndex = Int32.Parse(link1.CommandArgument);
}
protected void link2Click(object sender, EventArgs e)
{
    // Retrieve control
    LinkButton link2 = sender as LinkButton;
    int rowIndex = Int32.Parse(link2.CommandArgument);
}

抱歉!评论已关闭.