您的位置:首页 > 其它

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

2012-01-09 14:45 399 查看
<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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐