Alright, I have created Dynamic Image listing,
It will loop through a folder and search for image files,
then list the files as clickable ImageButton,
upon clicking on the image,
a new enlarged version will show up.
After hours of attempting to resolved this issue that I had,
I'd finally came to a solution for this problem.
I would like to share my finding with you.
First you'll need to write a method to handle the image look up
in this case I had written readFolderForImage(string path),
path is the location of your image folder, you can use direct
or Server.MapPath() to get the location.
Secondly, you'll need to create an onclick handle for your dynamic
button. For this I'd used, showSelected(object sender, ImageClickEventArgs e),
but there is a trick here, you must recast ImageButton type to sender object
if you wish to carry on the properties of the dynamic image.
Finally, run the method readFolderForImage(string path) on your page load
this will allow you to seek all level 1 image files in a folder. If you have SQL
database you can also read the root location of your images so you don't need
to type in a static text everytime.
using System.Io;
protected void Page_Load(object sender, EventArgs e)
{
//read the specified folder for images
readFolderForImage("E:\\images\\");
}
public void readFolderForImage(string path)
{
string[] dir = Directory.GetFiles(path);
string fullpath = null;
ImageButton img = new ImageButton();
//search folder for images
foreach (string f in dir)
{
fullpath = Path.GetFullPath(f);
img = new ImageButton();
//Set Image Attributes to ImageButton list
img.ImageUrl = fullpath;
img.Width = 30;
img.Height = 30;
img.Attributes.Add("runAt", "server");
img.CausesValidation = false;
img.Click += new ImageClickEventHandler(showSelected);
img.Parent.Controls.Add(img);
}
}
public void
showSelected(object sender, ImageClickEventArgs e)
{
//cast ImageButton on the sender object
ImageButton imgBtn = (ImageButton) sender;
Image img = new Image();
//Add a line separator from listed ImageButtons
img.ParentControls.Add(new LiteralControl("<hr/><center><br/><br/>"));
img.ParentControls.Add(new LiteralControl("<b>You have Selected</b><br/><br/>"));
//Set Image Attributes to enlarged selected image
img.Width = 128;
img.Height = 128;
img.ImageUrl = imgBtn.ImageUrl.ToString(); //this is where you inherit the url from your dynamic image
img.Parent.Controls.Add(img);
}
Enter your message below
Sign in or Join us (it's free).