简体   繁体   English

使用RowCommand / RowDeleting事件从文件夹/数据库中删除图像

[英]Delete image from Folder/Database using RowCommand/RowDeleting events

I am uploading images and display them in a Gridview .I save the images in a folder and the name in the database.Now I want if I uploaded 3 pictures and have to delete one which I select.So I have picture 1 2 3. When I select picture 2 than this will be deleted..How can I do that? 我正在上传图像并将它们显示在Gridview 。我将图像保存在文件夹中,并将数据保存在数据库中。如果我上传了3张图片并且必须删除我选择的图片,我想要。所以我有图片1 2 3。当我选择图片2时,这将被删除。我该怎么做? This is my code to upload my pictures: 这是我上传图片的代码:

string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
fileuploadimages.SaveAs(Server.MapPath("Images/" + filename));

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["baza_chestionar"].ToString());
con.Open();

SqlCommand cmd = new SqlCommand("Insert into Images(ImageName,ImagePath) values(@ImageName,@ImagePath)", con);
cmd.Parameters.AddWithValue("@ImageName", filename);
cmd.Parameters.AddWithValue("@ImagePath", "Images/" + filename);
cmd.ExecuteNonQuery();

con.Close();
Response.Redirect("~/upload.aspx");

To delete a file use System.IO.File.Delete : 要删除文件,请使用System.IO.File.Delete

File.Delete(Path.Combine(path, filename));

The rest of the implementation details, how you notify the server of the appropriate file to delete, is up to you. 其余的实现细节,即如何通知服务器要删除的适当文件,由您决定。

You can use below GridView's events to delete the images.. 您可以在GridView的事件下面使用它来删除图像..

  1. RowCommand RowCommand
  2. RowDeleting RowDeleting

Code Behind 代码背后

protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
{
    grd.Rows[e.RowIndex].FindControl("Control of the file Name"); 
    //Find control that contains file Name
    if (System.IO.File.Exists("FilePath"))
    {
        File.Delete(Path.Combine("path", "FileName"));
    }
    //Your Delete Code to delete record from database
}

protected void grd_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    grd.Rows[e.RowIndex].FindControl("Control of the file Name"); 
    //Find control that contains file Name
    if (System.IO.File.Exists("FilePath"))
    {
        File.Delete(Path.Combine("path", "FileName"));
    }
    //Your Delete Code to delete record from database
}

HTML HTML

<asp:GridView ID="grd" runat="server" onrowcommand="grd_RowCommand" 
onrowdeleting="grd_RowDeleting">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="delete" runat="server" Text="Select" CommandName="delete"></asp:LinkButton> 
                <asp:LinkButton ID="deleteRow" runat="server" Text="Select" CommandName="deleteRow"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

Please note that - you can use either RowCommand/RowDeleting Events...and I will go with RowDeleting 请注意-您可以使用RowCommand/RowDeleting事件...,而我将使用RowDeleting

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM