【问题标题】:How to display images in gridview when in database table we have stored image name only. How to give imgae path if it is inside a folder Eg. images在我们仅存储图像名称的数据库表中如何在gridview中显示图像。如果它在文件夹中,如何提供 imgae 路径。图片
【发布时间】:2018-05-03 17:47:55
【问题描述】:

在 C# 程序中,我需要在 gridview 的列 Eg.selected_image(column_name) 之一中插入图像标签。

图像存储在名为 product_imgs 的文件夹中。

在数据库中,我只存储了图像名称而不是完整路径,例如 product_imgs/

tbimg

imgcod imgnam

1 abc.jpg 2 pqr.jpg 等。

我无法在网格视图中显示这些图像。由于 gridview 中的控件必须具有 runat = server 标签才能工作(我可能在这里错了*)

我所做的是:(示例代码)

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    Width="1195px" BackColor="White" BorderColor="#336666" BorderStyle="Double"
    BorderWidth="3px" CellPadding="4" GridLines="Horizontal" Style="margin-left: 45px; margin-right: 44px;"
    DataSourceID="ObjectDataSource1"
    OnRowCommand="GridView1_RowCommand">
    <Columns>
        <asp:TemplateField HeaderText="Secondary Balloon(if any)">
            <ItemTemplate>
                <img id="sb" height="150" width="150" alt="SellerChoice Or Multicolor"
                    src='~/Product_Imgs/<%#Eval("addimg")%>' runat="server" />
            </ItemTemplate>
        </asp:TemplateField>
        <columns>
</asp:GridView>

提前致谢,祝您有美好的一天。

【问题讨论】:

  • 关于如何使用数据库存储 BLOB(包括图像)有两种思想流派,并尝试将两者结合起来 (simple-talk.com/sql/learn-sql-server/…)。您的方法要求文件位于作为正常 Web 服务器操作的一部分公开的文件夹中。自 HTML 出现以来,图像在 Web 服务器上的公开方式也是如此。

标签: c# asp.net image gridview


【解决方案1】:

在您的代码中,图片的 url 将不正确。这样做

<img src="/Product_Imgs/<%# Eval("addimg") %>" />

或者这个

<asp:Image ID="Image1" runat="server" ImageUrl='<%# "/Product_Imgs/" + Eval("addimg")  %>' />

【讨论】:

    猜你喜欢
    • 2014-08-05
    • 1970-01-01
    • 2013-12-15
    • 1970-01-01
    • 2014-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    相关资源
    最近更新 更多