6

I want to do something like this

<%#(DataBinder.Eval(Container, "DataItem.Age").ToString()=="0") 
    ?"n/a"
    :"DataBinder.Eval(Container, "DataItem.Age")"%>

is it possible?

p.campbell
  • 98,673
  • 67
  • 256
  • 322
vinit
  • 61
  • 1
  • 1
  • 2

2 Answers2

9

You can write a Method on page level and format the output there.

eg

<%# GetAgeDisplay(Eval("Age")) %>

and in code behind:

public String GetAgeDisplay(Int16 age) {
  return age == 0 ? "n/a" : String.Format("{0}", age );
}
ovm
  • 2,452
  • 3
  • 28
  • 51
7

Make sure you are calling DataBinder instead of simply returning a string:

Change this:

<%#(DataBinder.Eval(Container, "DataItem.Age").ToString()=="0") ? 
             "n/a":"DataBinder.Eval(Container, "DataItem.Age")"%>

To:

<%#(DataBinder.Eval(Container, "DataItem.Age").ToString()=="0") ? 
             "n/a":DataBinder.Eval(Container, "DataItem.Age")%>

What you are doing is returning a string instead of executing the binding expression.

Oded
  • 489,969
  • 99
  • 883
  • 1,009