0

can anybody please guide me how to display an image on gridview in asp.net? i had the image path get from database, eg : D:\Picture\photo.jpg

i had try this but it appear nothing in the image box. my front-end code:

<Columns>
    <asp:BoundColumn DataField="id" HeaderText="Email ID" Visible="False">
         <HeaderStyle  />
    </asp:BoundColumn>
    <asp:BoundColumn DataField="photo_url" HeaderText="Photo" Visible="false">
    </asp:BoundColumn>
    <asp:TemplateColumn>
        <ItemTemplate>
            <asp:Image ID="Image1" Height = "100" Width = "100" runat="server" DataImageUrlField = '<%# Eval("photo_url") %>' /> >>>>>>>>display image on this part
        </ItemTemplate>
    </asp:TemplateColumn>

    <asp:BoundColumn DataField="post_body" HeaderText="Post"></asp:BoundColumn>
    <asp:BoundColumn DataField="post_id" HeaderText="Comment/Post ID" Visible="False">
        <HeaderStyle Width="50px" />
    </asp:BoundColumn>
</Columns>
3
  • Did you get an error? Did the correct URL appear in the resulting HTML? Commented Jun 10, 2014 at 3:06
  • it is did not appear any error but when run it the image was not appear in the image column, just an empty column. the path get from database for photo_url is correct. it is D:\Projects\Orlig_FB_Service\Images\wall_post_551183598322481_10152973356489762.jpg Commented Jun 10, 2014 at 3:12
  • Yes, I know, you said that in your question. Did the correct URL appear in the resulting HTML? If you navigate to that URL manually, does the image appear? Commented Jun 10, 2014 at 3:13

1 Answer 1

3

Replace with this one:

  <asp:Image ID="Image1" Height = "100" Width = "100" runat="server" 
             ImageUrl='<%# ResolveUrl(Eval("photo_url").ToString()) %>' />
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your response, i tried it before but the same results comes out. Blank at the image column. if i hard-code the image url like this, ImageUrl='D:\Picture\photo.jpg' the image was appear, it is means do not hv any prob with the path
Ok, you need to have a relative path, so you need to check the value of photo_url in your database first. you need to make sure that the value format is look like this: ~/Picture/photo.jpg also make sure that you have the folder under your project solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.