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:
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);
}