Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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("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 ?

like image 905
Andreas Grech Avatar asked Jul 05 '09 10:07

Andreas Grech


4 Answers

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>
like image 153
Neil Fenwick Avatar answered Oct 24 '22 02:10

Neil Fenwick


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 %>>
like image 29
Shamozzle Avatar answered Oct 24 '22 02:10

Shamozzle


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("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.

like image 24
MikeD Avatar answered Oct 24 '22 01:10

MikeD


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>
like image 3
Zesxter Avatar answered Oct 24 '22 03:10

Zesxter