grandta13
grandta13

Reputation: 55

Conditional Operator in ASP.net Databound Field (C#)

I am trying to implement conditional formatting in a databound field in a DataList, but the error: Invalid expression term 'if' is being generated. My code is as follows:

<asp:DataList ID="dlItems" runat="server">
    <ItemTemplate>
        <asp:Label ID="lblDescription" runat="server" Text='
            <%# if (Eval("Description").ToString().Length <= 150)
            // The error is generated by the 'if' on the above line
                    Eval("Description");
                else
                    Eval("Description").ToString().PadRight(150).Substring(0,150).TrimEnd(); %>'>
        </asp:Label>
    </ItemTemplate>
</asp:DataList>

Note: The code in the else statement is largely irrelevant; I get the same error even when it's excluded.

Upvotes: 0

Views: 302

Answers (1)

Monah
Monah

Reputation: 6784

You can use the following

<asp:DataList ID="dlItems" runat="server">
    <ItemTemplate>
        <asp:Label ID="lblDescription" runat="server" Text='
            <%# Eval("Description").ToString().Length <= 150?Eval("Description"):          
                    Eval("Description").ToString().PadRight(150).Substring(0,150).TrimEnd() %>'>
        </asp:Label>
    </ItemTemplate>
</asp:DataList>

hope it will help you

Upvotes: 2

Related Questions