Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用RowCommand/RowDeleteing事件从文件夹/数据库中删除图像_C#_Asp.net - Fatal编程技术网

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

C# 使用RowCommand/RowDeleteing事件从文件夹/数据库中删除图像,c#,asp.net,C#,Asp.net,我正在上传图片,并将它们显示在网格视图中。我将图片保存在文件夹中,并将其名称保存在数据库中。现在我想上传3张图片,并且必须删除我选择的一张。因此,我有图片1 2 3。当我选择图片2时,这将被删除。我如何才能做到这一点?这是我上传图片的代码: string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName); fileuploadimages.SaveAs(Server.MapPath("Images/" + file

我正在上传图片,并将它们显示在
网格视图中
。我将图片保存在文件夹中,并将其名称保存在数据库中。现在我想上传3张图片,并且必须删除我选择的一张。因此,我有图片1 2 3。当我选择图片2时,这将被删除。我如何才能做到这一点?这是我上传图片的代码:

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");

要删除文件,请使用
System.IO.file.delete

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

其余的实现细节,即如何通知服务器要删除的适当文件,由您决定。

您可以使用下面的GridView事件来删除图像

  • RowCommand
  • 行删除

  • 代码隐藏
    HTML
    
    

    请注意-您可以使用
    RowCommand/rowdeleding
    Events…我将使用
    rowdeleding

    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
    }
    
    <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>