如何获取Gridview行的ID并在变量中传递该ID? [英] How Do I Get The Id Of Gridview Row And Pass That Id In A Variable?

查看:143
本文介绍了如何获取Gridview行的ID并在变量中传递该ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的aspx代码

Here is my aspx code

<pre lang="xml"><asp:TemplateField HeaderText="Submit" ItemStyle-HorizontalAlign="Center">
       <ItemTemplate>
       <asp:LinkButton ID="lnkSubmit" runat="server" CommandName="Submit" Text="Action" CommandArgument='<%# ((GridViewRow) Container).RowIndex %>' ></asp:LinkButton>
       </ItemTemplate>
       <EditItemTemplate>
       </EditItemTemplate>



这是我的aspx.cs代码


Here is my aspx.cs code

protected void GridView1_RowCommand(object Sender, GridViewCommandEventArgs e)
   {
       if (e.CommandName.Equals("Submit"))
       {
           GridViewRow FItem = (GridViewRow)((LinkButton)e.CommandSource).NamingContainer;
           int RowIndex = FItem.RowIndex;
           lblRowIndex.Text = "Row Index =" + RowIndex.ToString();
           Response.Redirect("InquiryReg.aspx");
       }

   }



我收到行索引但我无法得到行ID。


I am getting the Row Index but i cant get the row id.

推荐答案

最简单的解决方案是将ID存储在命令参数中:

The simplest solution would be to store the ID in the command argument:
<asp:LinkButton ID="lnkSubmit" runat="server" CommandName="Submit" Text="Action" CommandArgument='<%# Eval("ID") %>' />




protected void GridView1_RowCommand(object Sender, GridViewCommandEventArgs e)
{
   if (e.CommandName.Equals("Submit"))
   {
       string id = (string)e.CommandArgument;
       Response.Redirect("InquiryReg.aspx?id=" + Server.UrlEncode(id));
   }
}


这篇关于如何获取Gridview行的ID并在变量中传递该ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆