Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 341
Nerd Nord Avatar asked Feb 10 '23 01:02

Nerd Nord


2 Answers

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

like image 102
Abel C Avatar answered May 01 '23 08:05

Abel C


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.

like image 28
Pratik Gupta Avatar answered May 01 '23 09:05

Pratik Gupta