13

Hi all;

How to make inline eval if control ?

<asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <%# if(Eval("Bla Bla Bla").ToString().Length <= 15){Eval("Bla Bla Bla")}else{Eval("Bla Bla Bla").ToString().Substring(0,15)}%>
                </ItemTemplate>
            </asp:TemplateField>

1 Answer 1

33

Use the tertiary expression '?':

<asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <%# Eval("Bla Bla Bla").ToString().Length <= 15 ? Eval("Bla Bla Bla") : Eval("Bla Bla Bla").ToString().Substring(0,15) %>
                </ItemTemplate>
            </asp:TemplateField>
Sign up to request clarification or add additional context in comments.

4 Comments

<pedant> It's "ternary" (having three parts) </pedant> rather than "tertiary" (third in order) ;), or as Eric Lippert points out, just go with " The Conditional Operator": blogs.msdn.com/ericlippert/archive/2010/02/18/…
I could edit my post and correct it but than your comment wouldn't make sense any longer so I'll leave my post intact for all the world to see :)
edit away, I can always delete my comment, or look like a fool ;)
@edosoft, what if i use html code in this ? for example: <%#Eval("IsPopUp") ? %> <a href="url" onclick="function();">label</a> <% : %><a href="url" onclick="function2();">label2</a>

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.