开发者

Conditional operator with DataBinder.Eval

开发者 https://www.devze.com 2023-01-21 13:35 出处:网络
I want to do something like this <%#(DataBinder.E开发者_C百科val(Container, \"DataItem.Age\").ToString()==\"0\")

I want to do something like this

<%#(DataBinder.E开发者_C百科val(Container, "DataItem.Age").ToString()=="0") 
    ?"n/a"
    :"DataBinder.Eval(Container, "DataItem.Age")"%>

is it possible?


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 );
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消