Click here to Skip to main content
15,922,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have to sum txt1+txt2+txt3 int txt4.
user may or may not enter values in all the three text boxes.
i have taken below code from code project solved answer,but this is not working.
please help.
.cs code
C#
protected void txtlog_TextChanged(object sender, EventArgs e)
    {
        if ((!string.IsNullOrEmpty(txtlog.Text)) && (!string.IsNullOrEmpty(txtPole.Text)) && (!string.IsNullOrEmpty(txtDangri.Text)))
        {
            txtTotalQty.Text = (Convert.ToInt32(txtlog.Text) + Convert.ToInt32(txtPole.Text) + Convert.ToInt32(txtDangri.Text)).ToString();
        }
    }
    protected void txtPole_TextChanged(object sender, EventArgs e)
    {
        if ((!string.IsNullOrEmpty(txtlog.Text)) && (!string.IsNullOrEmpty(txtPole.Text))&& (!string.IsNullOrEmpty(txtDangri.Text)))
        {
            txtTotalQty.Text = (Convert.ToInt32(txtlog.Text) + Convert.ToInt32(txtPole.Text) + Convert.ToInt32(txtDangri.Text)).ToString();
        }
    }
    protected void txtDangri_TextChanged(object sender, EventArgs e)
    {
        if ((!string.IsNullOrEmpty(txtlog.Text)) && (!string.IsNullOrEmpty(txtPole.Text)) && (!string.IsNullOrEmpty(txtDangri.Text)))
        {
            txtTotalQty.Text = (Convert.ToInt32(txtlog.Text) + Convert.ToInt32(txtPole.Text) + Convert.ToInt32(txtDangri.Text)).ToString();
        }
    }


.aspx code:
ASP.NET
<asp:TextBox ID="txtlog" runat="server" Width="150px" 
                                                ontextchanged="txtlog_TextChanged">10
 <asp:RequiredFieldValidator ID="rvlog" runat="server"                    ControlToValidate="txtlog" Display="Dynamic"  SetFocusOnError="True">*

<asp:TextBox ID="txtPole" runat="server" Width="150px" 
 ontextchanged="txtPole_TextChanged">
  <asp:RequiredFieldValidator ID="rvPole" runat="server" 
   ControlToValidate="txtPole" Display="Dynamic" 
    SetFocusOnError="True">*


  <asp:TextBox ID="txtDangri" runat="server" Width="150px" 
  ontextchanged="txtDangri_TextChanged">
   <asp:RequiredFieldValidator ID="rvDangri" runat="server" 
    ControlToValidate="txtDangri" Display="Dynamic" 
    SetFocusOnError="True">*

<asp:TextBox ID="txtTotalQty" runat="server" Width="150px">
 <asp:RequiredFieldValidator ID="rvTotalQty" runat="server" 
  ControlToValidate="txtTotalQty" Display="Dynamic" 
   SetFocusOnError="True">*
Posted
Updated 13-May-14 7:37am
v2

You need to create a new asp file and add 4 asp text boxes to your project. Name those accordingly.
Then create a button to add sum to the 4th text box. Your assignment doesn't say button but that will be more efficient.

Asp Codes:
ASP.NET
<![CDATA[<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>]]>



<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <asp:textbox id="txt1" runat="server" xmlns:asp="#unknown"></asp:textbox>
        <asp:textbox id="txt2" runat="server" xmlns:asp="#unknown"></asp:textbox>
        <asp:textbox id="txt3" runat="server" xmlns:asp="#unknown"></asp:textbox>
        <br />
        <asp:button id="btnCalculate" runat="server" onclick="btnCalculate_Click" text="Calculate" xmlns:asp="#unknown" />
        </div>

        <div>

        <asp:textbox id="txt4" runat="server" ontextchanged="TextBox4_TextChanged" xmlns:asp="#unknown"></asp:textbox>
    
    </div>
    </form>
</body>
</html>


C# Codes like this:

C#
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 btnCalculate_Click(object sender, EventArgs e)
    {
        double num1 = Convert.ToDouble(txt1.Text);
        double num2 = Convert.ToDouble(txt2.Text);
        double num3 = Convert.ToDouble(txt3.Text);

        double result = num1 + num2 + num3;
        txt4.Text = result.ToString();

    }
}
 
Share this answer
 
Comments
ruby kaur 14-May-14 3:45am    
But i have to do it on same page and i can't use any button for it.
Please Help.
fatihkaratay 14-May-14 8:36am    
Entire stuff is going on the same page. If you can't have a button, how do you want to start an event? Like when the page loads or ?
There are a myriad of ways to do it. One easy way is to use Decimal.TryParse

C#
Decimal total, temp;

temp = 0;
Decimal.TryParse(txt1.Text, out temp);

total+=temp;

temp=0;

Decimal.TryParse(txt2.Text, out temp);
total+=temp;

...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900