【问题标题】:Using Delete Button from Gridview to delete a record from Database and from Folder使用 Gridview 中的删除按钮从数据库和文件夹中删除记录
【发布时间】:2016-07-21 18:06:45
【问题描述】:

当我单击删除时,包含图像的记录会从数据库中删除,但图像仍保留在文件夹中。请问有经验的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="删除 [产品] WHERE [ID] = @ID" InsertCommand="INSERT INTO [Product] ([Name]) VALUES (@Name)" SelectCommand="SELECT * FROM [产品]" UpdateCommand="UPDATE [Product] 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>
</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>

【问题讨论】:

  • @MMoore94,感谢您的贡献,onDelete 事件引发了错误。数据库中还有很多图像,Id 指向要删除的特定图像。您认为我下面的方法有什么问题: protected void grdProducts_RowDeleting(object sender, GridViewEditEventArgs e) { GridViewRow row = grdProducts.Rows[e.NewEditIndex]; int rowId = Convert.ToInt32(row.Cells[1].Text); File.Delete("~/Images/Products/" + rowId); }

标签: asp.net


【解决方案1】:

据我所知,您正在使用 &lt;asp:SqlDataSource &gt; 作为您的 gridview 的数据源。您的 SQL 命令已到位,显然您的删除命令有效,因为您说它会从数据库中删除它。

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

【讨论】:

  • 感谢您的建议 Peyote,我对这个问题有了更好的理解。但是,我的“RowDeleting”事件引发了一个错误,即“'grdProducts_RowDeleting' 没有重载匹配委托'System.Web.UI.WebControls.GridViewDeleteEventHandler'C:\Use...”下面是我的事件背后的代码。
  • protected void grdProducts_RowDeleting(object sender, GridViewEditEventArgs e) { // 存储单元格中的值 GridViewRow row2 = grdProducts.Rows[e.NewEditIndex]; // 需要完整路径的字符串 fileName = ("~/Images/Products/" + row2); if (fileName != null || fileName != string.Empty) { if ((System.IO.File.Exists(fileName))) { System.IO.File.Delete(fileName); } } }
【解决方案2】:

您需要将删除行添加到您的 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");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-13
    • 2013-01-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多