Is it possible to format a Textbox for currency just in ASP NET?

18,633

Solution 1

You can wrap TextBox in an <asp:UpdatePanel> and add AsyncPostBackTrigger of TextChanged event. You can verify, validate and change the string from the server side using that.

Solution 2

Yes you can:

string.Format("$ {0:#,##0.00}", double.Parse(textBox1.Text));

In this case it is globally ineffective, for it will display the $ sign, use commma (,) for a thousand separator and point/dot (.) for decimal. (You could change the currency symbol, but the separators will not change. Take into account that if the language you are developing for interchanges the separator dot for thousands and comma for decimals. Ex: Spanish)

string.Format(CultureInfo.CurrentCulture,"{0:C}", double.Parse(textBox1.Text));

This will give you currency, however this case depends on the culture you set up for the website. I know how to configure it by page: The first line of HTML code in the aspx file.

<%@ Page Language="C#" AutoEventWireup="true" Culture="es-HN" UICulture="es-HN" CodeBehind="yourpage.aspx.cs" Inherits="yoursite.yourpage" %>

There is a way to configure it in the web.config file but I do not know how. I encourage you to look for it. So far, I just include [Culture="es-HN" UICulture="es-HN"] on each page and it works. By the way, those values are for Honduras, so if you need other culture values look for them here: www.csharp-examples.net/culture-names/.

Solution 3

apparently this is not possible. I am waiting for people to disconfirm me. By the way this is how I solved:

textBox1.Text = string.Format("{0:#,##0.00}", double.Parse(textBox1.Text));
Share:
18,633

Related videos on Youtube

Nerd Nord
Author by

Nerd Nord

Updated on June 04, 2022

Comments

  • Nerd Nord
    Nerd Nord almost 2 years

    I have this textbox that should show currency values to 2 decimal numbers. I would like to do using the "Text" property of textboxes. Is that possible?

    <asp:TextBox ID="txtImponibile" runat="server" Width="120px" Enabled="False" ></asp:TextBox>
    

    Thanks.

    • Mivaweb
      Mivaweb almost 9 years
      Would you like to use a client side library like javascript or jquery or only ASP with using the ontextchanged event appennd on the textbox?
    • Nerd Nord
      Nerd Nord almost 9 years
      Hi, I'd like to use something like Text='<%#Eval(("value"),"{0:f2}")%>' just to display it rounded to two decimals.
    • brtb
      brtb almost 9 years
      as i understand you want only server side code to make your mask , but it causes postback for every textchange operation since as @Mivaweb mentioned about it you should use ontextchanged event
    • Nerd Nord
      Nerd Nord almost 9 years
      What if I wanted client side? I mean how could I do that?
  • Pratik Gupta
    Pratik Gupta almost 9 years
    for currency i recommend you parse the textbox text into a double format like this : double d = double.parse(txtImponibile.Text)