将动态图像叠加添加到数据绑定 Gridview

发布于 2024-10-19 09:57:19 字数 379 浏览 7 评论 0原文

我正在尝试在 ASP.Net (C#) 中的数据绑定 gridview 单元格内添加单元格的图像覆盖。该效果被设计为在图像中包含一个字符串,该字符串将位于从服务器拉取的图像之上。

IE:

数据库包含图像路径。数据网格根据相关项目的 ID 提取这些图像。我需要从数据库中提取价格并将其放置到放置在数据绑定图像单元格上的图像上。

SQL语句获取结果。 结果包含项目图像的路径。 商品有时会打折,因此需要特殊的覆盖层。 覆盖将有不同的价格覆盖它。

我试图将其分解为简单的部分。我不确定我是否想得太多,但我找不到可行的方法来做到这一点。由于ASP.Net中的数据网格控件,数据被转换为表格,并且在创建表格后没有任何方法可以确定ID。

如果有人有任何想法,我将不胜感激。

I am trying to add an image overlay of a cell within a data bound gridview cell in ASP.Net (C#). The effect is designed to have a string within the image that is going to sit on top of the image that is pulled from the server.

IE:

Database contains path to images. Data grid is pulling these images in based on the ID of the items in question. I need to pull the price from the database and lay it onto an image that is placed over the data bound image cell.

SQL Statement gets results.
Results contains path to image for items.
Items are sometimes on sale, so a special overlay is needed.
Overlay will have a varying price overlaying it.

I tried to break it down into easy portions. I am not sure if I am over thinking things or not but I can't find a feasible way to do this. The data is converted to a table due to the data grid control in ASP.Net, and there isn't any way to determine the IDs after the table is created.

If anyone has any ideas I would be most appreciative.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

愁杀 2024-10-26 09:57:19

一种方法是使用 CSS 将单元格的背景设置为图像,并将价格以纯文本形式写入同一单元格(显示渲染输出):

<td class="product"  
    style="background: url(path-to-image.png) top left no-repeat">  
    <span class="price">$ 3.22</span>  
</td>  

这种方法的唯一缺点是单元格的大小必须明确调整为与图像相同的尺寸,您可能不知道。

另一种选择是在单元格内使用 元素,并仍然使用 表示纯文本价格(显示渲染输出):

<td class="product">
    <img src="path-to-image.png" alt="..."/>
    <span class="price">$ 3.22</span>
</td>

无论哪种方法,您将使用 CSS 来定位单元格中的价格并设置其样式:

td.product { 
    position: relative
}
span.price {  
    position: absolute;
    top : 40px;    < whatever works for you
    left: 40px;    <  
}

一个非常简单的带有模板的 GridView 示例

您需要将 GridView 的 AutoGenerateColumns 属性设置为 false,并自行处理列创建和模板化。

<asp:GridView ID="products" runat="server"  
              AutoGenerateColumns="false"  
              DataKeyNames="productId"  
              DataSourceID="ObjectDataSource1">
    <Columns>
        <asp:BoundField DataField="productId" HeaderText="Product ID" />
        <asp:TemplateField HeaderText="Whatever ...">
            <ItemTemplate>
                <img src='<%# DataBinder.Eval(Container.DataItem,"productImageUrl") %>'
                     alt="..." />
                <span class="price">
                    <%# DataBinder.Eval(Container.DataItem,"productPrice","{0:c}") %>
                </span>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="productDesc" HeaderText="Description" />
    </Columns>
</asp:GridView>

One way to do this is to set the cell's background to the image using CSS and write the price as plain text into the same cell (rendered output displayed):

<td class="product"  
    style="background: url(path-to-image.png) top left no-repeat">  
    <span class="price">$ 3.22</span>  
</td>  

The only downside to this approach is that the cell would have to be explicitly sized to the same dimensions as the image, which you may not know.

Another option is to use an <img> element inside the cell and still use a <span> for the plain text price (rendered output displayed):

<td class="product">
    <img src="path-to-image.png" alt="..."/>
    <span class="price">$ 3.22</span>
</td>

Regardless of which method, you would use CSS to position and style the price within the cell:

td.product { 
    position: relative
}
span.price {  
    position: absolute;
    top : 40px;    < whatever works for you
    left: 40px;    <  
}

A very simple GridView example with templating

You will need to set the GridView's AutoGenerateColumns property to false and handle the column creation and templating yourself.

<asp:GridView ID="products" runat="server"  
              AutoGenerateColumns="false"  
              DataKeyNames="productId"  
              DataSourceID="ObjectDataSource1">
    <Columns>
        <asp:BoundField DataField="productId" HeaderText="Product ID" />
        <asp:TemplateField HeaderText="Whatever ...">
            <ItemTemplate>
                <img src='<%# DataBinder.Eval(Container.DataItem,"productImageUrl") %>'
                     alt="..." />
                <span class="price">
                    <%# DataBinder.Eval(Container.DataItem,"productPrice","{0:c}") %>
                </span>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="productDesc" HeaderText="Description" />
    </Columns>
</asp:GridView>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文