Question

I am trying to do the following in ASP.NET 3.5. Basically, I am binding a LINQDataSource to a DataList. There is a property called "Deleted" and if it is true, I want to display different markup. The following code throws errors:

<asp:DataList runat="server">
    <ItemTemplate>
        <% If CBool(Eval("Deleted")) Then%> 
            ...
        <% Else%>
            ...
        <% End If%>
    </ItemTemplate>
</asp:DataList>

Is this possible? If not, what are the alternatives?

Was it helpful?

Solution

Why not just use the RowDataBound event and check the value of your fields then? RowDatabound is ideal for these situations where you want to alter data in a gridview based on values in the result set.

RowDataBound Event from MSDN

OTHER TIPS

I might suggest keeping the code-front lean and writing out the desired text via a function result:

<asp:DataList runat="server">
    <ItemTemplate>
         <%# GetText(Container.DataItem) %>
    </ItemTemplate>
</asp:DataList>

And the code-behind:

protected static string GetText(object dataItem)
{        
    if (Convert.ToBoolean(DataBinder.Eval(dataItem, "Deleted"))
        return "Deleted";

    return "Not Deleted";
}

I hope it helps.

One option as a work-around would be to utilise a panel.

<asp:DataList runat="server">
    <ItemTemplate>
        <asp:Panel Visible="<%# Eval("Deleted") %>">
            ...(deleted content here)...
        </asp:Panel>
        <asp:Panel Visible="<%# Not Eval("Deleted") %>">
            ...(other content here)...
        </asp:Panel>
    </ItemTemplate>
</asp:DataList>

Perhaps use the ItemDataBound event of a datalist. For gridview's its the rowdatabound event that is ideal for altering display of values based on other values in the result set. ItemDataBound event

So basically on itemdatabound you can play around with your conditionals. Again, this is an educated guess since I've typically done this with the RowDataBound event on gridview's.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top