【问题标题】:GridView Delete Confirm ButtonFieldGridView 删除确认按钮字段
【发布时间】:2015-03-14 22:49:18
【问题描述】:

我想通过 javascript 确认从我的 gridview 中删除一行。

我的网格视图看起来像:

<asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand">
        <Columns>
            <asp:ButtonField CommandName="delete" Text="Delete" />
        </Columns>
    </asp:GridView>

我已经尝试过使用 TempleField 并且它可以工作,但问题是获取要从数据库中删除的选择行的值。

我试过这样:

<asp:GridView ID="GridView1" runat="server" onrowcommand="GridView1_RowCommand">
        <Columns>
            <asp:TemplateField>
            <ItemTemplate>
                <asp:Button ID="btnSave" runat="server" Text="Delete" OnClientClick="return confirm('Do you want to delete?')" CommandName="delete" />
            </ItemTemplate>
        </asp:TemplateField>
        </Columns>
    </asp:GridView>

并尝试获取这样的值:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
        if (e.CommandName == "delete")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            string id = GridView1.Rows[index].Cells[1].Text.ToString();
            Response.Write(id);
        }
}

但它会在

上引发 FormatException
int index = Convert.ToInt32(e.CommandArgument);

有人可以帮助我吗?抱歉,我的英语不好。

【问题讨论】:

  • “我试过这样” 那么成功了吗?如果没有,发生了什么?
  • 需要在按钮标签中添加CommandArgument='&lt;%# Eval("Yourrowidfieldvalue") %&gt;'
  • 好的,我现在添加了那个错误:“ArgumentOutOfRangeException”在线'string id = GridView1.Rows[index].Cells[1].Text.ToString();'
  • 你添加了什么作为你的命令参数?如果您将其用作索引,请使用 &lt;%# ((GridViewRow) Container).RowIndex %&gt;
  • 我使用的方式与您在第二个示例中的方式相同。但在我确认的最后,我使用了;,删除的是Delete。工作正常。

标签: c# asp.net


【解决方案1】:

您需要从按钮标签中设置CommandArgument

 <asp:Button ID="btnSave" runat="server" Text="Delete" OnClientClick="return confirm('Do you want to delete?')" CommandName="delete" CommandArgument="<%# ((GridViewRow) Container).RowIndex %>"/>

Here 是关于如何使用 CommandArgument 的更多信息

【讨论】:

  • 感谢您的帮助,感谢尝试它的人。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-26
  • 1970-01-01
  • 2021-04-15
  • 2012-09-17
  • 1970-01-01
  • 2021-12-19
  • 1970-01-01
相关资源
最近更新 更多