LCJ
LCJ

Reputation: 22662

Eval Formatting using ASP.Net Markup (only)

I have a repeater. One of the items in the repeater is a label. This label should display the TaxRate value. I am able to do this using the following markup.

<asp:Label ID="lblTaxRate" runat="server" Text = '  <%# Eval("TaxRate")  %>'></asp:Label>

Now, I need to add a $ sign just before the tax rate value. How can we do it using ASP.Net markup only?

Note: Solution using javascritpt is not anticipated.

Note: I cannot replace label with any other control or get rid of the label

Upvotes: 1

Views: 16117

Answers (5)

znn
znn

Reputation: 501

Many years later, working on some legacy code, I have a similar issue. The overload for the format on Eval, can be also used for any custom string.

On my scenario:

//Displayed regardless there is a value to evaluate or not.
<%#Eval("ContractLength") + " Years" %>

//Displayed only when there is a value to evaluate.
<%#Eval("ContractLength","{0} Years") %>

On your scenario:

//Displayed regardless there is a value to evaluate or not.
<%#"$" + Eval("TaxRate") %>

//Displayed only when there is a value to evaluate.
<%#Eval("TaxRate","${0}") %>

Upvotes: 1

Rob Salmon
Rob Salmon

Reputation: 21

This works, and takes into account cultural differences in currency

'<%# Eval("TaxRate","{0:C2}") %>'

Upvotes: 2

LCJ
LCJ

Reputation: 22662

Thanks to @nunespascal answer.

<asp:Label ID="lblTaxRate" runat="server" Text = '<%# String.IsNullOrEmpty(Convert.ToString(Eval("TaxRate"))) == true ? "" : Eval("TaxRate", "${0}")%>  ' > </asp:Label>

IMPROVEMENT

<%# String.Format("{0:C}", Eval("Amount") ) %>

or

<%# ((double)Eval("Amount")).ToString("C") %>

Upvotes: 1

nunespascal
nunespascal

Reputation: 17724

This should do it:

<asp:Label ID="lblTaxRate" runat="server" Text = '  <%# Eval("TaxRate", "${0}") %>'></asp:Label>

Upvotes: 2

tomfanning
tomfanning

Reputation: 9670

Try:

<%# DataBinder.Eval(Container.DataItem, "TaxRate", "{0:c}") %>

(from here)

Upvotes: 3

Related Questions