繁体   English   中英

GridView删除确认ButtonField

[英]GridView Delete Confirm ButtonField

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

我的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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM