删除链接按钮使用ASP在GridView中不起作用

时间:2015-03-31 22:33:09

标签: asp.net visual-studio-2012 gridview

所以没有太多要解释的,我有一个GridView,我放了一个删除按钮,询问你是否确定要在点击它时删除。我正在使用VisualStudio2012,而且我已经在很多其他页面中完成了这个,但我从来没有遇到过这个问题。

GridView的:

<asp:GridView ID="MaintenanceTable" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Maintenance_ID" DataSourceID="MaintDataSource" EmptyDataText="There are no data records to display." BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical" Width="1000px">
    <AlternatingRowStyle BackColor="#DCDCDC" />
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton runat="server" ID="DeleteButton" Text="Delete"
                    CommandName="delete"
                    OnClientClick="if (!window.confirm('Are you sure you want to delete this item?')) return false;" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:CommandField ShowEditButton="True" ShowSelectButton="True" />
        <asp:BoundField DataField="Maintenance_ID" HeaderText="Maintenance_ID" InsertVisible="False" ReadOnly="True" SortExpression="Maintenance_ID" />
        <asp:CheckBoxField DataField="Status" HeaderText="Status" SortExpression="Status" />
        <asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
        <asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
    </Columns>
    <EditRowStyle BackColor="#999999" />
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <HeaderStyle BackColor="#34397D" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#0000A9" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>

这是我在GridView中删除的唯一代码:

<asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton runat="server" ID="DeleteButton" Text="Delete"
                    CommandName="delete"
                    OnClientClick="if (!window.confirm('Are you sure you want to delete this item?')) return false;" />
            </ItemTemplate>
        </asp:TemplateField>

1 个答案:

答案 0 :(得分:0)

您需要添加以下代码:

protected void MaintenanceTable_RowCommand(object sender, 
                     GridViewCommandEventArgs e)
{
  if (e.CommandName == "Delete")
  {
    // get the maintenanceId of the clicked row
    int maintenanceId = Convert.ToInt32(e.CommandArgument);
    // Delete the record 
    DeleteRecordByID(maintenanceId);
    // Implement
   }
}

请注意,确保使用“删除”而不是“删除”。

检查此资源GridView Delete, with Confirmation