Barnabeck Barnabeck - 16 days ago 6
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?
Martin

Answer

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"/>