Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional operator with DataBinder.Eval

I want to do something like this

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

is it possible?

like image 840
vinit Avatar asked Oct 21 '10 14:10

vinit


2 Answers

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 );
}
like image 171
ovm Avatar answered Nov 07 '22 13:11

ovm


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.

like image 26
Oded Avatar answered Nov 07 '22 13:11

Oded