开发者

Conditional statement in databound expression

I want to display an image if 2 conditions are met.

  1. The data item is not null
  2. The value of the data item is greater than 0

Markup

<img id="Img1" runat="server" visible='<%#IIF( DataBinder.Eval(Container.DataItem,    
"amount") is DBNull.Value Or DataBinder.Eval(Container.DataItem, 
"amount") = 0, False, True)%>' src="/Images/check.png" />

Error message

Operator '=' is not defined for type 'DBNull' and type 'Integer'. Description: An unhandled exception occurred during the execution of the current web requ开发者_运维问答est. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Operator '=' is not defined for type 'DBNull' and type 'Integer'.


Try using OrElse. In VB.Net the Or conditional operator causes both sides to evaluate regardless of the success. So if you have a null it's going to attempt the comparison anyway. Using OrElse will cause the second condition not to be evaluated if the first is true.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜