ASP.Net: Conditional Logic in a ListView’s ItemTemplate

I want to show certain parts of an ItemTemplate based according to whether a bound field is null. Take for example the following code:

(Code such as LayoutTemplate have been removed for brevity)

<asp:ListView ID="MusicList" runat="server">
    <ItemTemplate>
        <tr>
            <%
                if (Eval("DownloadLink") != null)
                {
            %>
            <td>
                <a href="<%#Eval(" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"DownloadLink") %>">Link</a>
            </td>
            <%
                } %>
        </tr>
    </ItemTemplate>
</asp:ListView>

The above gives the following run-time error:

Databinding methods such as Eval(),
XPath(), and Bind() can only be used
in the context of a databound control.

So how can put some conditional logic (like the above) in an ItemTemplate ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

What about binding the “Visible” property of a control to your condition? Something like:

<asp:ListView ID="MusicList" runat="server">
   <ItemTemplate>
    <tr runat="server" Visible='<%# Eval("DownloadLink") != null %>'>
        <td>
            <a href='<%#Eval("DownloadLink") %>'>Link</a>
        </td>
    </tr>
   </ItemTemplate>
</asp:ListView>

Method 2

To resolve “The server tag is not well formed.” for the answers involving visibility, remove quotes from the Visible= parameter.

So it will become:

<tr runat="server" Visible=<%# Eval("DownloadLink") != null ? true : false %>>

Method 3

I’m not recommending this as a good approach but you can work around this issue by capturing the current item in the OnItemDataBound event, storing it in a public property or field and then using that in your conditional logic.

For example:

<asp:ListView ID="MusicList" OnItemDataBound="Item_DataBound" runat="server">
    <ItemTemplate>
        <tr>
            <%
                if (CurrentItem.DownloadLink != null)
                {
            %>
            <td>
                <a href="<%#Eval(" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener"DownloadLink") %>">Link</a>
            </td>
            <%
                } %>
        </tr>
    </ItemTemplate>
</asp:ListView>

And on the server side add the following code to your code behind file:
public MusicItem CurrentItem { get; private set;}

protected void Item_DataBound(object sender, RepeaterItemEventArgs e)
{
   CurrentItem = (MusicItem) e.Item.DataItem;
}

Note that this trick will not work in an UpdatePanel control.

Method 4

If you have 2 different structure that are to be rendered according to a condition then use panels

<asp:ListView ID="MusicList" runat="server">
    <ItemTemplate>
        <tr>
            <asp:Panel ID="DownloadNull" runat="server" Visible="<%# Eval("DownloadLink") == null %>" >
            <td> Album Description BlaBlaBla <img src="../images/test.gif"> </td>
            </asp:Panel>

            <asp:Panel ID="DownloadNotNull" runat="server" Visible="<%# Eval("DownloadLink") != null %>" >
            <td> Album Description BlaBlaBla <img src="../images/test.gif">
                <a href='<%# Eval("DownloadLink")' >Download</a>
                ..... 
            </td>
            </asp:Panel>
        </tr>
    </ItemTemplate>
</asp:ListView>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x