views:

2029

answers:

4

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?

+1  A: 

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

RandomNoob
+1  A: 

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.

RandomNoob
I agree with the educated guess, and the link to the ItemDataBound event does a good job of explaining it. Ben Griswold's route will also work; However, I find it a little cleaner and more flexible to hook into the DataList's ItemDataBound event as mentioned.
codefoo
A: 

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>
Aydsman
+1  A: 

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.

Ben Griswold