Click here to Skip to main content
16,022,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 directories with some images – 1 directory with full-size image and 1 with thumbnails (same filename in both)

I load the thumbnail images to some image buttons in a Panel and when I click on one of the image buttons I want to see the full-size image in a new window. I do this for faster loading.

When I click a image button I see the thumbnail in a new window (pure quality), so my question is:

How do I change the ImageURL when clicking a image button so I see the full size image and not the thumbnail?

What I have tried:

Here is my code:
C#
private void UploadImage()
{
    foreach (string strFileName in Directory.GetFiles(Server.MapPath("~/Images/Thumb/")))
    {
        ImageButton imageButton = new ImageButton();
        FileInfo fileInfo = new FileInfo(strFileName);
        imageButton.ImageUrl = "~/Images/Thumb/" + fileInfo.Name;
        imageButton.Width = Unit.Pixel(100);
        imageButton.Height = Unit.Pixel(100);
        imageButton.Style.Add("padding", "5px");
        imageButton.Click += new ImageClickEventHandler(imageButton_Click);
        
        Panel1.Controls.Add(imageButton);
    }
}

void imageButton_Click(object sender, ImageClickEventArgs e)
{
    Response.Redirect("~/ShowImage.aspx?ImageURL=" + ((ImageButton)sender).ImageUrl);
}

I have tried to put in below 2 lines code in the "ImageButton_Click" so it looks like this:
C#
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    ImageButton imageButton = (ImageButton)sender;
    imageButton.ImageUrl = "~/Images/" + fileInfo.Name;
    Response.Redirect("default.aspx");
}

but I didn't get the fileInfo.Name from "private void UploadImage()" it gives a build error: "The name 'fileInfo' does not exists in the current context"

Thanks in advance!
Posted
Updated 16-Jul-24 0:53am
v2

Firstly, as you have discovered, a local variable defined in one method is not available from another method.

And since your local variable is defined within a loop, it's not even available outside of the loop in the same method.

You need to learn about C# scopes:
Basic concepts - C# language specification | Microsoft Learn[^]


Secondly, your click event handler is redirecting to the default.aspx page. That means any changes you make to the controls on the current page will be thrown away.

You need to learn about how ASP.NET works, particularly the page life-cycle:
ASP.NET Page Life Cycle Overview | Microsoft Learn[^]
The ASP.NET Page Lifecycle – A Basic Approach[^]


For what you're trying to do, you will need to extract the filename from the ImageUrl property, and then use it to construct the path of the full-size image - eg:
C#
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
    ImageButton imageButton = (ImageButton)sender;
    string imageUrl = imageButton.ImageUrl;
    imageButton.ImageUrl = imageUrl.StartsWith("~/Images/Thumb/", StringComparison.OrdinalIgnoreCase)
        ? "~/Images/" + imageUrl.Substring(15)
        : "~/Images/Thumb/" + imageUrl.Substring(9);
}
 
Share this answer
 
Thanks alot!
My mistake about redirecting to default.aspx

Here is my solution and it works perfect:
 private void UploadImage()
 {
     
    foreach (string strFileName in Directory.GetFiles(Server.MapPath("~/Images/Thumb/")))
    {
        ImageButton imageButton = new ImageButton();
        FileInfo fileInfo = new FileInfo(strFileName);
        imageButton.ImageUrl = "~/Images/Thumb/" + fileInfo.Name;
        imageButton.Width = Unit.Pixel(100);
        imageButton.Height = Unit.Pixel(100);
        imageButton.Style.Add("padding", "5px");
        imageButton.Click += new ImageClickEventHandler(imageButton_Click);
        
        Panel1.Controls.Add(imageButton);
    }
    

}

void imageButton_Click(object sender, ImageClickEventArgs e)
{
    ImageButton imageButton = (ImageButton)sender;
    string imageUrl = imageButton.ImageUrl;
    imageButton.ImageUrl = imageUrl.StartsWith("~/Images/Thumb/", StringComparison.OrdinalIgnoreCase)
        ? "~/Images/" + imageUrl.Substring(15)
        : "~Images/Thumb/" + imageUrl.Substring(9);
    Response.Redirect("~/ShowImage.aspx?ImageURL=" + ((ImageButton)sender).ImageUrl);
}
 
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