删除GridView行并以编程方式创建列

时间:2015-03-24 15:40:33

标签: asp.net

我使用数据绑定除了数据源。我想添加列并在该列上使用删除功能来删除所选行。我正在使用select选项获取所选行的值。现在我在行单击或行选择上遇到问题。

1 个答案:

答案 0 :(得分:0)

向gridview添加删除按钮:

<asp:TemplateField>
    <ItemTemplate>
        <asp:Button ID="btnDelete" runat="server" Text="Delete" CommandName="DeleteRow" CommandArgument='<%#Eval("ID")%>' OnClientClick="return confirm('Are you sure?');" CausesValidation="false" />
    </ItemTemplate>
</asp:TemplateField>

向gridview添加行命令事件处理程序:

OnRowCommand="gv_RowCommand"

将删除逻辑添加到命令事件处理程序:

protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "DeleteRow")
    {
        //Delete the record using e.CommandArgument as the ID
        gv.DataBind() //rebind the grid so that the deleted row is gone
    }
}

注意:命令名称不能为“删除”,因为它是为“自动”删除

保留的
相关问题