Kgn-web
Kgn-web

Reputation: 7555

Why is my conditional execution in my aspx page not returning the correct result?

I have ASP.Net(aspx) where I need to render the html based on some condition. This is how my aspx looks like.

<%# Eval("FileName").ToString()  == "pdf" ?"True":"False"%>

This is returning string True or False based on the condition however I need to render the html as below.

<%# Eval("FileName").ToString()  == "pdf" ?"":"<img src='<%# DataBinder.Eval(Container.DataItem, 'FilePathUrl')%>' />"%>

How do I address this?

Upvotes: 2

Views: 60

Answers (2)

Andrey Sidorov
Andrey Sidorov

Reputation: 1

You can use "if" statement instead of "?:"

<% if (Eval("FileName").ToString() != "pdf")%>
<% { %>
    <img src='<%# DataBinder.Eval(Container.DataItem, 'FilePathUrl')%>
<% } %>

Upvotes: 0

VDWWD
VDWWD

Reputation: 35514

You need to create the string for the image like this:

<%# Eval("FileName").ToString() == "pdf" ? "" : "<img src=\"" + Eval("FilePathUrl").ToString() + "\">" %>

You cannot nest databinding expressions like you are doing.

Upvotes: 3

Related Questions