使用Gridview中的“删除”按钮从“数据库”和“文件夹”中删除记录

时间:2016-07-21 18:06:45

标签: asp.net

当我点击删除时,包含图像的记录将从数据库中删除,但图像仍保留在文件夹中。任何经验丰富的asp.net程序员都可以帮忙吗? 提前谢谢!

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Management.aspx.cs" Inherits="pages_Management_Management" %>

                  添加新产品     

    

    <asp:GridView ID="grdProducts" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
        DataKeyNames="ID,Image" DataSourceID="generalProducts" Width="80%" OnRowEditing="grdProducts_RowEditing" CellPadding="4" 
        ForeColor="#333333" GridLines="None">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
            <asp:BoundField DataField="CatID" HeaderText="CatID" SortExpression="CatID" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
            <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
            <asp:BoundField DataField="Image" HeaderText="Image" SortExpression="Image" />
            <asp:BoundField DataField="Seller" HeaderText="Seller" SortExpression="Seller" />
            <asp:BoundField DataField="StateID" HeaderText="StateID" SortExpression="StateID" />
            <asp:BoundField DataField="CityID" HeaderText="CityID" SortExpression="CityID" />
            <asp:BoundField DataField="StreetID" HeaderText="StreetID" SortExpression="StreetID" />
            <asp:BoundField DataField="StreetNoID" HeaderText="StreetNoID" SortExpression="StreetNoID" />
            <asp:BoundField DataField="Type" HeaderText="Type" SortExpression="Type" />
            <asp:BoundField DataField="Packaging" HeaderText="Packaging" SortExpression="Packaging" />
            <asp:BoundField DataField="Unit" HeaderText="Unit" SortExpression="Unit" />
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>

”      DeleteCommand =“DELETE FROM [Product] WHERE [ID] = @ID”      InsertCommand =“INSERT INTO [Product]([Name])VALUES(@Name)”      SelectCommand =“SELECT * FROM [Product]”      UpdateCommand =“UPDATE [Product] SET [Name] = @Name WHERE [ID] = @ID”&gt;

 <DeleteParameters>
        <asp:Parameter Name="ID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="Name" Type="String" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="Name" Type="String" />
        <asp:Parameter Name="ID" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>
</p>
<p>
    &nbsp;</p>
<p>
    <asp:LinkButton ID="LinkButton2" runat="server" CssClass="button" PostBackUrl="~/pages/Management/ManageProductCats.aspx">Add New ProductCat</asp:LinkButton>
</p>
<asp:GridView ID="grdProductsType" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="ID" DataSourceID="generalProductCat" Width="50%" style="margin-top: 0px" CellPadding="4" ForeColor="#333333" GridLines="None">
    <AlternatingRowStyle BackColor="White" />
    <Columns>
        <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
        <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
    </Columns>
    <EditRowStyle BackColor="#2461BF" />
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
    <RowStyle BackColor="#EFF3FB" />
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    <SortedAscendingCellStyle BackColor="#F5F7FB" />
    <SortedAscendingHeaderStyle BackColor="#6D95E1" />
    <SortedDescendingCellStyle BackColor="#E9EBEF" />
    <SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
<asp:SqlDataSource ID="generalProductCat" runat="server" ConnectionString=
    "<%$ ConnectionStrings:SuperStoreDBConnectionString %>"
     DeleteCommand="DELETE FROM [ProductCat] WHERE [ID] = @ID" 
    InsertCommand="INSERT INTO [ProductCat] ([Name]) VALUES (@Name)"
     SelectCommand="SELECT * FROM [ProductCat]" 
    UpdateCommand="UPDATE [ProductCat] SET [Name] = @Name WHERE [ID] = @ID">
    <DeleteParameters>
        <asp:Parameter Name="ID" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
        <asp:Parameter Name="Name" Type="String" />
    </InsertParameters>
    <UpdateParameters>
        <asp:Parameter Name="Name" Type="String" />
        <asp:Parameter Name="ID" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>
    </div>

2 个答案:

答案 0 :(得分:0)

据我所知,您使用<asp:SqlDataSource >作为gridview的数据源。您的SQL命令已经到位,显然您的删除命令有效,因为您说它从数据库中删除它。

我没有看到任何代码,所以我认为没有任何代码。您需要在GridView的“RowDeleting”事件中添加代码。可以在那里添加代码来访问文件系统并从你正在谈论的任何文件夹中删除文件。

答案 1 :(得分:0)

您需要在gridview标记上添加行删除,并在c#中创建删除文件的方法

ASP.NET

<asp:GridView ID="gv" runat="server" OnRowDeleting="true">

C#

protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    File.Delete("put your directory path here");
}