C# .NET - multiply two textbox values

Asked By joseph on 23-May-11 06:07 AM
actually i want to show sum of values in third textbox aonce as the value entered in first and second textbox in asp.net page

protected void TextBox3_TextChanged(object sender, EventArgs e)
    {
        TextBox3.Text = (Convert.ToInt32(TextBox1.Text) * Convert.ToInt32(TextBox2.Text)).ToString();
    }

i tried like this but itz not working out,plz help me 
Reena Jain replied to joseph on 23-May-11 06:10 AM
hi,

try like this

protected void TextBox3_TextChanged(object sender, EventArgs e)
{
if(TextBox1.Text!="" && TextBox2.Text!="")
{
 TextBox3.Text = ((Convert.ToInt32(TextBox1.Text)) * (Convert.ToInt32(TextBox2.Text))).ToString();
}
}

let me know whether is working for you or not
joseph replied to Reena Jain on 23-May-11 06:15 AM
hi tnk u ,but itz not wrking.

is the click event correct should v write it in textbox3 click event? and my requirement is while i enter 2  in textbox1 and 3 in textbox2 the textbox3 should display 6 in it automatically .plz can u help me out

dipa ahuja replied to joseph on 23-May-11 06:18 AM
float value1 = float.Parse(textBox1.Text.ToString());
float value2 = float.Parse(textBox2.Text.ToString());
textBox3.Text = (value1 * value2).ToString();
Jem Savery replied to joseph on 23-May-11 06:22 AM
Hi Joseph,

you need to either write the logic on Textbox3's TextChanged event or you should write the logic on Button's click event.

If you are using TextBox's textChanged event then your declaration code will be

<asp:TextBox ID="TextBox3" runat="server" AutoPostBack="true" OnTextChanged="TextBox3_TextChanged"></asp:TextBox>

protected void TextBox3_TextChanged(object sender, EventArgs e)
    {
        if (TextBox1.Text != "" && TextBox2.Text != "")
        {
            TextBox3.Text = ((Convert.ToInt32(TextBox1.Text)) * (Convert.ToInt32(TextBox2.Text))).ToString();
        }
    }

or if you are using button, then your event will be like this

<asp:Button ID="btnCalculate" runat="server" Text="Multiply" OnClick="btnCalculate_Click" />

protected void btnCalculate_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text != "" && TextBox2.Text != "")
        {
            TextBox3.Text = ((Convert.ToInt32(TextBox1.Text)) * (Convert.ToInt32(TextBox2.Text))).ToString();
        }
    }

Hope this helps.

Ravi S replied to joseph on 23-May-11 06:23 AM
HI

try this code

textBox3.Text = (Decimal.Parse(textBox1.Text) * Decimal.Parse(textBox2.Text)).ToString();
refer the link for different ways
http://stackoverflow.com/questions/3528501/how-to-multiply-text-box-values-with-javascript
http://forums.asp.net/t/1340946.aspx/1
Jem Savery replied to Jem Savery on 23-May-11 06:23 AM
For TextChanged event, you must add AutoPostBack="True" attribute to your textbox.

Hope this helps.
Riley K replied to joseph on 23-May-11 06:23 AM
Use the below code

public
YourForm() { InitializeComponent(); //add the handler for the two input boxes textBox1.TextChanged += textbox_TextChanged; textBox2.TextChanged += textbox_TextChanged; }   private void textBox_TextChanged(object sender, EventArgs e) { try { textBox3.Text = (Decimal.Parse(textBox1.Text) * Decimal.Parse(textBox2.Text)).ToString(); } catch { //an error occured... } }
Devil Scorpio replied to joseph on 23-May-11 06:26 AM
Hi Joseph,

please use the following code under lost focus event of Textbox2

if
(TextBox1.Text!="" && TextBox2.Text!="")
{
 TextBox3.Text = ((Convert.ToInt32(TextBox1.Text)) * (Convert.ToInt32(TextBox2.Text))).ToString();
}
Asked By joseph on 23-May-11 06:28 AM
should i write dis code in textbox2 or textbox3 cilck event?
Reena Jain replied to joseph on 23-May-11 08:20 AM
hi,

for this you have to write code on textbox2 textchange event or on leave event of textbox2 like this

protected void TextBox2_TextChanged(object sender, EventArgs e)
{
if(TextBox1.Text!="" && TextBox2.Text!="")
{
 TextBox3.Text = ((Convert.ToInt32(TextBox1.Text)) * (Convert.ToInt32(TextBox2.Text))).ToString();
}
}

hope this will help you, let me know
joseph replied to Reena Jain on 23-May-11 09:25 AM
Thanks  everybody u guys helped me a lot.. what ever u told worked well
mithilesh kumar replied to Reena Jain on 11-Jul-13 09:05 AM
protected void txt_price_TextChanged(object sender, EventArgs e) { Double total = Convert.ToInt32(txt_quantity.Text) * Convert.ToDouble(txt_price.Text); txt_total.Text = total.ToString(); }
mithilesh kumar replied to joseph on 11-Jul-13 09:07 AM
yes