【问题标题】:Fetching the id from the first column of GridView从 GridView 的第一列获取 id
【发布时间】:2011-01-12 08:12:44
【问题描述】:

我有一个网格,我在其中每行显示一些记录。它是这样的:

现在,我的问题是,当我按下查看按钮时,我想从会话变量的第一列中获取 ID,以便可以在下一页上显示相同的 ID。

对于EditButton的ItemTemplate,我使用的是这段代码:

<ItemTemplate>
   <asp:LinkButton ID="EditBtn" CssClass="btn green" CommandName="edit" ToolTip="Edit" Text="Edit" runat="server" />
</ItemTemplate>

【问题讨论】:

  • AutoGenerateColumn 属性设置在什么值上?你的数据源是什么样子的?
  • 第一列中的 ID 不是自动生成的,而是从数据库中获取的。因此,它们可以是随机的,不一定按照我上面写的顺序。这是 GirdView HTML 源代码的链接。 pastebin.com/2Zj2DhJU

标签: asp.net gridview


【解决方案1】:

您可以尝试将其作为命令参数传递:

<ItemTemplate>
    <asp:LinkButton 
        ID="EditBtn" 
        CssClass="btn green" 
        CommandName="edit" 
        CommandArgument='<%# Eval("FirstColumnId") %>'
        OnCommand="EditCommand"
        ToolTip="Edit" 
        Text="Edit" 
        runat="server" />
</ItemTemplate>

在后面的代码中:

protected void EditCommand(object sender, GridViewCommandEventArgs e)
{
    var id = e.CommandArgument;
    // TODO: do something with the id
}

【讨论】:

    【解决方案2】:

    试试这个。您的 aspx 无需更改

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Session["UserID"] = ((Label)GridView1.Rows[e.NewEditIndex].FindControl("lb1")).Text.Trim();
    }
    

    其他方法(使用 DataKeyNames - 首选)

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        Session["UserID"] = GridView1.DataKeys[e.NewEditIndex].Value.ToString();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多