Select all images using ASP.NET

前端 未结 7 782
轮回少年
轮回少年 2020-12-06 16:52

I am new to ASP.NET and C#. I am trying to retrieve all images from folder and show it on page, but it\'s only selecting one image.

My ASP.NET code:

         


        
7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 17:39

    The problem is in this statement:

    Image.ImageUrl = Convert.ToString(dr["Image_Path"]);
    

    What does this statement do? It assigns each image path value to only one Image.ImageUrl. So, the Image.ImageUrl will hold the last assigned image path. The result is only one picture will be displayed. This is not what you want.

    What you want is: show all pictures --> assign each image path to each Image.ImageUrl --> dynamically create the Image and add it to the form. So, instead of writing that statement, you should do something like:

    Image img = new Image();
    img.ImageUrl = dr["Image_Path"].ToString();
    img.AlternateText = "Test image";
    form1.Controls.Add(img);
    

    The code is not tested. Just focus on the idea. You can do it like this, or use the repeater, or your own way, it's up to you.

    Feel free to ask me if you find something unclear :)

提交回复
热议问题