views:

365

answers:

3

If I have this:

<img ID="imgField" runat="server" ImageUrl='<%# DataBinder.Eval(Container.DataItem,"Name") %>' />

How can I add "images/" or any other string add on to the url?

I tried

ImageUrl=' "images/" + <%# DataBinder.Eval(Container.DataItem,"Name") %>'

And

ImageUrl= "images/" + '<%# DataBinder.Eval(Container.DataItem,"Name") %>'

And

ImageUrl='images/<%# DataBinder.Eval(Container.DataItem,"Name") %>'

But none of them worked. Anyone know?

+2  A: 
<img ID="imgField" runat="server" ImageUrl='<%# string.Format("images/{0}", DataBinder.Eval(Container.DataItem,"Name")) %>' />

Or, if you want to shorten it up a bit...

<img ID="imgField" runat="server" ImageUrl='<%# string.Format("images/{0}", Eval("Name")) %>' />

and I think this should work too - but don't have a project in front of me to test it on...

<img ID="imgField" runat="server" ImageUrl='<%# Eval("Name", "images/{0}") %>' />
Scott Ivey
A: 
<img ID="imgField" runat="server"
  ImageUrl='<%# "images/" + DataBinder.Eval(Container.DataItem,"Name") %>' />

Or inside a gridview, you could use an ImageField column:

<asp:GridView>
  <Columns>
    <asp:ImageField DataImageUrlField="Name"
      DataImageUrlFormatString="images/{0}" />
  </Columns>
</asp:GridView>
M4N
A: 

You were using <% instead of the correct: <%#

JBrooks
You're correct about needing the #, however I did already have it in my application, I just didn't write it in my example. Thanks!
Matt