简体   繁体   English

带有DataBinder.Eval的条件运算符

[英]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? 可能吗?

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: 确保您正在调用DataBinder而不是简单地返回一个字符串:

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. 你正在做的是返回一个字符串而不是执行绑定表达式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM