tags:

views:

413

answers:

5

I have a label (literal, whatever), that is being filled with currency ($5000.00). Is there an easy way to style the cents, like make the font-size smaller.

I know I can split the two up, but I am using MVP, and am looking for an easy way than passing all these new properties to the control (4+ of these labels are present).

Open to any suggestions, like a new control instead of the label, whatever you got.

+1  A: 

Ideally you need to format it like the following.

$5000.<span class="Cents">00</span>

How you accomplish this is a bit tricky, you can have a "Dollars" and a "Cents" property and then manipulate the string into the format.

Mitchel Sellers
+3  A: 

I would suggest creating a new control that simply extends to Label/Literal. You can call it MoneyLabel, though Literal would probably be easier to work with. From that point, you can override the Render method to change the HTML output, format the string as you like.

Referencing it is not that tricky, the pages would need to reference the assembly where the class is stored.

In this way, you are not introducing CSS and HTML from the codebehind, not having to create extra JavaScript from what you need, and this is extremely reusable and its into how you code .NET already

xximjasonxx
I know you beat @scottschulthess with the answer, his did provide code, so I gave him the credit. +1 though.
Martin
Thanks Martin, and for the record I didn't steal the idea from @xximjasonxx anyways :)
scottschulthess
+1  A: 

You could use javascript and let the client do all the heavy lifting...eg, in jquery (you'd have to define the putSpansAroundDollarsAndCents() function)

<div class="currencyToSplit">$5000.00</div>

$(".currencyToSplit").each(function() { putSpansAroundDollarsAndCents($(this)) });
Chris Shaffer
I thought about this a few minutes ago as well, as it would be the simplest way (at least as far as I could think of).
Martin
+2  A: 

I suppose in an ideally semantic world you'd probably do something like:-

<span class="currency-symbol">£</span>
<span class="major-currency-unit">5000</span>
<span class="decimal-point">.</span>
<span class="minor-currency-unit">00</span>

and then style away to your heart's content.

AdamRalph
The problem with this is, while I know splitting the cents from the dollar amount is a reasonable solution, I was looking for one where I didn't need to split them.
Martin
I can't see how you can avoid splitting them into separate output elements if you want to apply specific styling for each. Apart from some nasty JavaScript method of course.
AdamRalph
+1  A: 

I would inherit from the label class, then create a newlabel class, StyledMoneyLabel. I haven't tested this code, but hopefully you'll get the idea.

class StyledMoneyLabel : Label
{
    double money = 0.0;

    Render(HtmltextWriter)
    {
        HtmlSpan dollars = new HtmlSpan(CSS_CLASS);
        dollars.Text = money.ToString(FORMAT_STRING_FOR_GETTING_DOLLARS.
        ///etc for decimals and so forth
     }
}
scottschulthess