简体   繁体   English

通过GridView从文件夹中删除图像

[英]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: 我正在尝试通过获取其路径名来删除图像,但它始终返回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. 但是我需要图像的完整路径来删除它,我尝试使用Path.GetFullPath(url),它不起作用。 Need help on this. 需要帮助。

And heres the aspx html side: 这是aspx html的一面:

 <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. 如果gridview行将处于编辑模式,则只有您可以找到<EditItemTemplate>标记内的控件。 in editmode, you will get rowindex as -1. 在editmode下,您将得到rowindex为-1。 in other mode, rowindex will be greater than -1, then you can find controls that are inside <ItemTemplate> tag. 在其他模式下,rowindex将大于-1,然后您可以找到<ItemTemplate>标记内的控件。 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(); 
}

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

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