0

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.

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, that did it. To make sure I understand how this works, I would like to ask if the following assumptions are correct: ? is used to end the test statement. | : is used to start an else clause. Also, I would like to ask where I can find more information on the DataBinder.Eval() method. My searches so far have mostly only returned documenation on the basic use of the Eval() method.
conditional operator is : condition ? first_expression : second_expression; if the condition was true then first expression is evaluated else the second expression becomes the result, for more information check here msdn.microsoft.com/en-us/library/ty67wk28.aspx
Thanks, I'd not seen that before and it seems very useful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.