3

我知道关于这个主题有很多问题,但我的情况有点不同。我试图在一个数据列表单元格中显示 3 个图像,例如,我在 SQL Server 中有一个可以有多个图像的产品表。

 Image Mapping table 
 ID     URL_Mapping_ID ProductID  
 1      image 1.png    1
 2      image 2.png    1
 3      image 3.png    1   

 Product Table 
 ProductID   Product
 1           Chips

从 SQL 中选择它后,结果是多行但具有不同的图像。

现在在我的 asp 数据列表中,我必须显示 1 个带有所有图像的产品,以便用户可以放大缩略图。

实施此方案的最佳方法是什么?

4

2 回答 2

1

您需要使用 HttpHandler 并将 IsReusable 属性设置为 true 以适应多个图像:

使用 HttpHandler 流式传输数据库图像

public class FeaturedHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        ...
    }

    public bool IsReusable
    {
        get
        {
            return true;
        }
    }
}

绑定图像

<img class="mainEventsImage" 
    src='<%# Eval("MainImagePath").ToString().Replace("\\", "/") %>' 
        alt='<%# Eval("Title") %>' runat="server" />

由于您可能事先不知道每条记录有多少图像,因此您必须在代码隐藏中动态创建图像控件:

在服务器端动态创建项目模板

于 2012-10-25T19:33:42.323 回答
1

我做了一个小演示,演示如何在dataList 中显示 dataList。对于您给定的示例,我在产品 dataList 中创建了另一个图像dataListItemTemplate

<asp:DataList ID="dlProducts" runat="server" DataKeyField="ProductID" DataSourceID="sqlProducts">
    <HeaderTemplate>
        <table>
        <thead>
            <tr>
                <th>ProductID</th>
                <th>Product</th>
                <th>Images</th>
            </tr>
        </thead>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td><asp:Label ID="ProductIDLabel" runat="server" Text='<%# Eval("ProductID") %>' /></td>
            <td><asp:Label ID="ProductLabel" runat="server" Text='<%# Eval("Product") %>' /></td>
            <td>
                <asp:DataList ID="dlImages" runat="server" DataKeyField="ID" DataSourceID="sqlImages">
                    <ItemTemplate>
                        <img src='<%# Eval("URL_Mapping_ID") %>' width="20px" height="20px" />
                    </ItemTemplate>
                </asp:DataList>
                <asp:SqlDataSource runat="server" ID="sqlImages" ConnectionString='<%$ ConnectionStrings:ConnectionString %>' ProviderName='<%$ ConnectionStrings:ConnectionString.ProviderName %>' SelectCommand="SELECT * FROM [Image] WHERE ([ProductID] = @ProductID)">
                    <SelectParameters>
                        <asp:ControlParameter ControlID="ProductIDLabel" PropertyName="Text" Name="ProductID" Type="Int32"></asp:ControlParameter>
                    </SelectParameters>
                </asp:SqlDataSource>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:DataList>
<asp:SqlDataSource ID="sqlProducts" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Products]"></asp:SqlDataSource>

此解决方案可能又快又脏,因此欢迎提供任何提示或提示!仅供参考:运行此演示会导致此结果。

给定解决方案的输出

于 2012-10-25T19:58:50.703 回答