简体   繁体   中英

Deleting images from folder by gridview

I have a grid view which I populated with images from a folder. I am trying to delete the image by getting their path name but it always returns me null:

Here are my codes to populate the grid view with images:

 protected void GetImage()
    {
        string path = HttpContext.Current.Request.PhysicalApplicationPath + @"Story/Food Fit For A King";
        string[] files = System.IO.Directory.GetFiles(path, "*.jpg");


        IList<ImageFileInfo> imageFileList = new List<ImageFileInfo>();
        foreach (string strFileName in files)
        {
            // Change the Absolute path to relative path of File Name and add to the List
            imageFileList.Add(new ImageFileInfo { FileName = ResolveUrl(strFileName.Replace(Server.MapPath("/"), "~/")) });
        }

        gvStory.DataSource = imageFileList;
        gvStory.DataBind();
    }

And below is my code to delete :

  protected void gvStory_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        GridView gvQuestion = (GridView)sender;
        int row = e.RowIndex;

             // Extract Values.
       // string imageName = (TextBox)gvStory.Rows[row].Cells[0].FindControl("TextBox1");// RETURNS NULL 
        Image img = (Image)gvStory.Rows[row].Cells[0].FindControl("Image1");
        string url = img.ImageUrl;
     //   string fileName = Path.GetFullPath(url); // RETURNS NULL 

  //string fileName = Path.Combine(Server.MapPath(@"Story/Food Fit For A King"), imageName);

        File.Delete(fileName);

        GetImage(); 


    }

Am I doing the correct way to the the filepath of the image? But I need the full path of the image to delete it, I tried to use Path.GetFullPath(url) , it doesn't work. Need help on this.

And heres the aspx html side:

 <asp:TemplateField HeaderText="Images">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("FileName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("FileName") %>' Width="240" Height="160" />
                    </ItemTemplate>
                </asp:TemplateField>

Need help on this.

If gridview row will be in edit mode, then only you can find the control that are inside <EditItemTemplate> tag. in editmode, you will get rowindex as -1. in other mode, rowindex will be greater than -1, then you can find controls that are inside <ItemTemplate> tag. Otherwise you will get null values. So, you can try this way,

protected void gvStory_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    GridView gvQuestion = (GridView)sender;
    int row = e.RowIndex;
    string fileName ="";
    if(row==-1)
    {
       string imageName = (TextBox)gvStory.Rows[row].Cells[0].FindControl("TextBox1");
       fileName = Path.Combine(Server.MapPath(@"Story/Food Fit For A King"),imageName);
    }
    else
    { 
       Image img = (Image)gvStory.Rows[row].Cells[0].FindControl("Image1");
       string url = img.ImageUrl;
       fileName = Path.GetFullPath(url); 
    }
    File.Delete(fileName);
    GetImage(); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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