background-image eval

2019-08-27 15:50发布

问题:

I am using DataList to show thumbnails, how can I use background-image url with eval

This code gives me The server tag is not well formed error.

<asp:DataList ID="DataListPortfolio" runat="server" RepeatColumns="3">
        <ItemTemplate>

<asp:Image ID="ImageButton1" runat="server" 
           style="background-image: url('<%#Eval("featuredImagesSmall")%>');" />

        </ItemTemplate>
</asp:DataList>

回答1:

According to your comments, you probably just want to use a div instead of <asp:Image (which renders as an img), in order to achieve thumbnails that are all the same size regardless of the image size:

<asp:DataList ID="DataListPortfolio" runat="server" RepeatColumns="3">
    <ItemTemplate>
        <div style='width:100px;height:100px;background-position:center;background-image:url(<%# Eval("featuredImagesSmall") %>)'></div>
    </ItemTemplate>
</asp:DataList>

I just put an arbitrary height and width on the div, but that will ensure all the thumbnails are the same size. You can play with the CSS to position the image inside the div.



回答2:

Why not use the ImageUrl property of the ASP.NET Image control? Something like this:

<asp:DataList ID="DataListPortfolio" runat="server" RepeatColumns="3">
        <ItemTemplate>

<asp:Image ID="ImageButton1" runat="server" ImageUrl='<%# Eval("featuredImagesSmall")%>' />

        </ItemTemplate>
</asp:DataList>