Community discussion forum

My Solution, Your Tutorial: Dynamic ImageButton C# ASP.NET

  • 2 years ago

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

    Post was edited on 12/03/2009 18:47:58 Report abuse
  • 9 months ago
    dont' think this works.. when I tried to compile it give me an error "fp" is not defined..
  • 8 months ago
    there's a coding error fp is supposed to be fullpath :) sorry about that
  • 8 months ago
    Thanks tkruvgt , Really appreciate it. But I have another question here as well? In " img.ParentControls.Add(new LiteralControl("


    "));" Is this a special kind of control , am I overlooking something? Or is this suppose to be "img.Parent.Controls.Add(new LiteralControl("


    "));" with "." in between Parent and Controls?
  • 8 months ago
    Would you be able to place the html as well as the code behind file once you have compiled it?

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback