Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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();
    }


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 hope these codes help!
thanks in advance
Posted
Updated 3-Nov-17 16:02pm

1 solution

try
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;
               }
           }
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900