【问题标题】:How to display image on gridview when image path get from database从数据库获取图像路径时如何在gridview上显示图像
【发布时间】:2020-08-11 23:05:12
【问题描述】:

谁能指导我如何在 asp.net 的 gridview 上显示图像? 我从数据库中获取了图像路径,例如:D:\Picture\photo.jpg

我试过这个,但它在图像框中什么也没有。 我的前端代码:

<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>

【问题讨论】:

  • 您遇到错误了吗?生成的 HTML 中是否出现了正确的 URL?
  • 它没有出现任何错误,但是当运行它时,图像没有出现在图像列中,只是一个空列。从数据库获取 photo_url 的路径是正确的。它是 D:\Projects\Orlig_FB_Service\Images\wall_post_551183598322481_10152973356489762.jpg
  • 是的,我知道,您在问题中说过。正确的 URL 是否出现在生成的 HTML 中?如果您手动导航到该 URL,图像会出现吗?

标签: asp.net gridview datagridview


【解决方案1】:

用这个替换:

  <asp:Image ID="Image1" Height = "100" Width = "100" runat="server" 
             ImageUrl='<%# ResolveUrl(Eval("photo_url").ToString()) %>' />

【讨论】:

  • 感谢您的回复,我之前尝试过,但结果相同。图片栏空白。如果我像这样对图像 url 进行硬编码,ImageUrl='D:\Picture\photo.jpg' 图像出现了,这意味着路径没有任何问题
  • 好的,你需要一个相对路径,所以你需要先检查你的数据库中photo_url的值。您需要确保值格式如下所示:~/Picture/photo.jpg 还要确保您的项目解决方案下有该文件夹
  • 非常感谢阿里先生 :)
猜你喜欢
  • 1970-01-01
  • 2012-11-25
  • 2018-11-08
  • 1970-01-01
  • 2013-02-22
  • 1970-01-01
  • 2015-02-12
  • 2016-04-29
  • 1970-01-01
相关资源
最近更新 更多