MasterOfDisaster MasterOfDisaster - 2 months ago 21
ASP.NET (C#) Question

How to create a dynamic src within a foreach loop?



<%foreach (Art a in SearchedArt)%>
<%{ %>
<tr>
<td><asp:Image ID="Image1" runat="server" src=<%a.PicturePath %>/></td>
<td><%= Global.Users.Find(m => m.UserID == a.UserID).Name %></td>
<td>
<ul>
<%foreach (string tag in a.Tags) %>
<%{ %>
<li><%= tag %></li>
<%} %>
</ul>
</td>
</tr>
<%} %>





So what I am trying to do is display a different image for each piece of art in a table. However, src=<%a.PicturePath%> doesn't seem to be doing the trick (I tried it with quotations and it didn't work either). How do I do this?

Answer Source

The asp:Image is a server-side tag - you need to set its ImageUrl property from code behind or use a client-side <img> and set its src attribute:

<img src="<% =a.PicturePath %>" />