Barnabeck Barnabeck - 1 year ago 103
ASP.NET (C#) Question

suppress unit display in label

I usually use this syntax to display the unit symbol in a label: ej. 5'230 €

<asp:Label runat="server" Text='<%# Eval("TotalAmount","{0:#,###.##}&nbsp;€") %>' id="LabelTotal"/>

The ugly thing about is that the symbol is displayed even if the value is null or empty. Then I would like the symbol just to be suppressed.

Anyone has an idea how to do that?

Answer Source

You can use the following snippet

<asp:Label runat="server" Text='<%# Convert.ToDecimal(Eval("TotalAmount")) > 0 ? string.Format("{0:C}", Convert.ToDecimal(Eval("TotalAmount"))) : string.Empty %>' id="LabelTotal"/>