Use the 'If condition' in Item Template [A

2020-01-30 02:41发布

问题:

I have two elements; <img> and <video> in an ItemTemplate, I want to test the value <% # Eval ("Url")%> to choose an element to display.

<itemtemplate>
    <li>
        <video data-cycle-cmd="pause" id='my-video' class='video-js' controls preload='auto' width='980' height='452' poster='MY_VIDEO_POSTER.jpg' data-setup='{}'>
            <source src='<%# Eval("Url") %>' type='video/mp4'>
        </video>  
    </li>
    <li>
         <img src="<%# Eval("url") %>">   
    </li>
</itemtemplate>

回答1:

You can use a ternary operator in the template and based on that outcome show/hide the correct element.

<ItemTemplate>
    <li>
        <asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible='<%# Eval("SomeValue").ToString() == "video" ? true : false %>'>

            <video data-cycle-cmd="pause" id='my-video' class='video-js' controls preload='auto' width='980' height='452' poster='MY_VIDEO_POSTER.jpg' data-setup='{}'>
                <source src='<%# Eval("Url") %>' type='video/mp4'>
            </video>

        </asp:PlaceHolder>

        <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("Url") %>' Visible='<%# Eval("SomeValue").ToString() == "video" ? false : true %>' />
    </li>
</ItemTemplate>


标签: asp-classic