0

how can i dynamically display my images coming from database? I'll be using a the image inside a list

<ul>
   <li>
      <asp:Image id="image1" runat="server" ImageUrl=""/>
   </li>
</ul>

I'm using C# on my code behind. I already created a method to display images but i dont know how can i use my method inside the list and in the image control. Do i need to use repeater to achieve this? What i want to happen is to repeat the list as many as the numbers of my images in my database.

David Hedlund
  • 128,221
  • 31
  • 203
  • 222
nhoyti
  • 1,615
  • 2
  • 26
  • 42

3 Answers3

2

A Repeater is definitely your best solution. This is how you'd set it up:

<asp:Repeater runat="server" ID="Repeater">
    <HeaderTemplate>
       <ul>
    </HeaderTemplate>
    <ItemTemplate>
        <li>
            <asp:Image id="image1" runat="server" 
                ImageUrl='<%# DataBinder.Eval(Container.DataItem, "Image") %>' />
        <li>
    </ItemTemplate>
    <FooterTemplate>
        </ul>
    </FooterTemplate>
</asp:Repeater>

Then all you need to do is assign the DataSource and the Repeater will handle the rest:

DataTable databaseResults = GetYourImages();
Repeater.DataSource = databaseResults;
Repeater.DataBind();
djdd87
  • 67,346
  • 27
  • 156
  • 195
0

Yes, you should use a repeater.

However, you don't use your method to display image inside the repeater. Instead, you should provide an URL to your image. The browser will first download your HTML, then it will extract all image URLs from it, and then it will download each image, one by one, using their URLs.

Therefore, you have to create another page (call it, say, GetImage.aspx) that will return your image and put URL to that page in your <li> tags.

Fyodor Soikin
  • 78,590
  • 9
  • 125
  • 172
0

You need to build an image handler. E.g. write an ASPX page that takes image id and returns mime type of image/jpeg or image/png with binary data.

http://myServer/myApp/ImageHandler.aspx?id=32we3df9wekly2

Ref:

  1. Images in database vs file system
  2. display/retrieve image from sql database in vb.net
Community
  • 1
  • 1
ankitjaininfo
  • 11,961
  • 7
  • 52
  • 75