如何在数据库表格中显示图像时我们只存储图像名称。如果它在文件夹Eg中,如何给出imgae路径。图片

时间:2021-02-03 00:26:20

In a C# program, I'm required to insert an image tag in one of the gridview's column Eg.selected_image(column_name).

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

the images are stored in a folder named product_imgs.

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

in the database, I have stored only the image names not the complete path eg product_imgs/

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

tbimg

imgcod imgnam

1 abc.jpg 2 pqr.jpg etc..

1 abc.jpg 2 pqr.jpg等..

I'm having trouble displaying these images in the grid view. As the control inside gridview must have runat = server tag in order to work (i might be wrong here*)

我在网格视图中显示这些图像时遇到问题。因为gridview中的控件必须有runat = server标签才能工作(我可能在这里错了*)

what I have done is:(Sample Code)

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

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

Thanks In Advance And have an amazing day.

在此先感谢并度过了美好的一天。

1 个解决方案

#1


0  

In your code, the url of the image will be incorrect. Do this

在您的代码中,图像的网址将不正确。做这个

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

Or this

或这个

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

#1


0  

In your code, the url of the image will be incorrect. Do this

在您的代码中,图像的网址将不正确。做这个

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

Or this

或这个

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