Link to home
Start Free TrialLog in
Avatar of techques
techques

asked on

How to display a text of textbox in a label in real time?

I use ASP.NET and C#

There is a asp:textbox and a asp:label

When I input 15092009 in textbox, it needs to show $15,092,009.00 in real time.

How can I do it?

use javascript or ASPX?

Avatar of Krummelz
Krummelz
Flag of South Africa image

You should use AJAX. Put your textbox and label inside an ajax UpdatePanel control. Then, OnTextChanged of the textbox, you can update your label, and format the value to a currency like this:

decimal myValue = txtMyTextbox.Text;
lblMyLable.Text = decimal.ToString("{0:c}");

You have to put it into a decimal variable first before you can format it to currency.
Avatar of techques
techques

ASKER

I have no experience using ajax, can you show me the full code in aspx?
Sure, this would be the aspx file:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <asp:TextBox ID="myTextBox" runat="server" OnTextChanged="myTextBox_OnTextChanged"></asp:TextBox><br/>
            <asp:Label ID="lblMyLabel" runat="server"></asp:Label>
        </asp:UpdatePanel>    
    </div>
    </form>
</body>
</html

Open in new window

And this would be the aspx.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void myTextBox_OnTextChanged(object, sender, EventArgs e)
    {
        decimal myValue = txtMyTextbox.Text;
        lblMyLabel.Text = decimal.ToString("{0:c}");
    }
}

Open in new window

SOLUTION
Avatar of Krummelz
Krummelz
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Snarf0001
You should DEFINATELY use javascript, not aspx.  
You'll have to do a bit more manual work to format as currency, but for something this basic, there's no reason to have the extra processing / time / postback that AJAX would cause.
Ajax is javascript.... and if you're developing in asp.net then its the easiest option to rather use the built in ajax controls as opposed to manually coding something like that.
i am aware that ajax is javascript, but it uses js to post back to the server and back to the page.
though its easier to do it that way, its far less efficient.  the js (non-ajax) for something like that would not be too complicated, and would give much better performance.
Hi

It has Parser error and cannot compile:
Unknown server tag 'asp:ScriptManager'

And, there is no UpdatePanel.

protected void myTextBox_OnTextChanged(object sender, EventArgs e)
{
            decimal myValue = myTextBox.Text;  
            lblMyLabel.Text = myValue.ToString("{0:c}");  //should it be myValue but not decimal.ToString?
}

I found there is a AjaxControlToolkit.dll in bin folder. Do I need to add any reference in the project? as it cannot recognize ScriptManager and UpdatePanel.

Also, why it needs using System.Linq; ?



Oh i see.. then how about some code for us to use?
not a problem, in the office in a few minutes, will post js then
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for both help. excellent solution. I add more points.
Snarf0001 has provided an excellent js solution, thanks for great help.