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

Asp.net 使用ASP在GridView中删除链接按钮不起作用,asp.net,visual-studio-2012,gridview,Asp.net,Visual Studio 2012,Gridview,所以没有什么要解释的,我有一个GridView,我放了一个删除按钮,当你点击它时,它会询问你是否确定要删除。我使用的是VisualStudio2012,我在很多其他页面上都这样做过,但我从来没有遇到过这个问题 GridView: <asp:GridView ID="MaintenanceTable" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="

所以没有什么要解释的,我有一个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>
<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>
这确实是我在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>

您需要添加以下代码:

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
   }
}
以防万一,请确保使用Delete而不是Delete

检查此资源