从gridview到imagebox显示图像

从gridview到imagebox显示图像

本文介绍了从gridview到imagebox显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨所有

i有一个网格视图和文件上传器,如下所示







和文件上传工作正常用户可以将文件发送到我网站的文件夹中查看网格视图中文件的名称,但问题是当用户点击选择链接或其中一个按钮(一个用于删除,一个用于显示)时我想要的问题

图像显示在下面的图像框中文件上传器

和我的代码支持gridview获取数据



hi all
i have a grid-view and file up-loader just like below

Screen

and file uploader work well user can send the file in the folder of my website and see the name of the file in the grid view but the problem is i want when user click on Select Link or one of that buttons (one for delete and one for show)
the image shows in the imagebox below of the file uploader
and this my code behind for gridview getting data

private void getimages()
    {
        string[] filePaths = Directory.GetFiles(Server.MapPath("~/GalleryFiles/"), "*.jpg");
        List<ListItem> files = new List<ListItem>();
        foreach (string filePath in filePaths)
        {
            string fileName = Path.GetFileName(filePath);

            files.Add(new ListItem(fileName, "~/GalleryFiles/" + fileName));
        }
        GridView1.DataSource = files;
        GridView1.DataBind();
    }





我的尝试:



此函数提供GalleryFiles目录中的所有.jpg文件并在gridview中显示,用户可以删除它们但我想在图像框中显示它们

此代码为删除文件



What I have tried:

this function give all of the .jpg files from the GalleryFiles Directory and shows in the gridview and user can delete them but i want to show them in the image box
this code for delete files

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{


        if (e.CommandName == "DeleteFile")
        {
            var index = Convert.ToInt32(e.CommandArgument);

            var row = GridView1.Rows[index];
            //select that column has filepath
            var filePath = row.Cells[0].Text;

            var fullFileName = Request.PhysicalApplicationPath + @"\GalleryFiles\" + filePath;

            if (File.Exists(fullFileName))
            {
                // Delete file
                File.Delete(fullFileName);


            }
            // Refresh data
            getimages();
        }

}



i希望这些代码有所帮助!

提前感谢


i hope these codes help!
thanks in advance

推荐答案

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
       {
           if (e.CommandName == "Select") // name of the command
           {
               var index = Convert.ToInt32(e.CommandArgument);
               var row = GridView1.Rows[index];
               var filePath = row.Cells[2].Text; // zero based index
               var fullFileName = Path.Combine( Server.MapPath( "GalleryFiles") + filePath);
               if (File.Exists(fullFileName))
               {
                   Image1.ImageUrl = fullFileName;
               }
           }
       }


这篇关于从gridview到imagebox显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 21:43