2015-03-31 68 views
0

所以没有太多解释,我有一个GridView,并且我放了一个删除按钮,询问您是否确定要删除它时单击它。我使用VisualStudio2012,并且我在其他许多页面中都做了这个,但是我从来没有遇到过这个问题。删除链接按钮不能在GridView中使用ASP

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> 

回答

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