2015-03-31 90 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