Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
hai dear friends,
i hav 3 textbox on my webpage...if i enter value in two text ,then automatically the third textbox shd get the subtracted value of these two textbox value...Can u help me?????????
Posted
Updated 23-Aug-11 0:43am
v2

ASP.NET
<asp:Textbox id="textbox2" runat="server" onBlur = "Javascript:SubstactData();" /> 


JavaScript
<script>
  function SubstactData()
  {
    var txt1 = document.getElementById('textbox1');
    var txt2 = document.getElementById('textbox2');
    var txt3 = document.getElementById('textbox3');

    txt3.value =  parseInt(txt1.value) - parseInt(txt2.value);
  }
</script>



Mark as answer if solved your problem
 
Share this answer
 
v3
Okay . This is easy enough. If you want to maintain it from Js You need to pass its CLientID. I am giving you an example.

This is my Js file. I have included this file in the Master Page. Have a look Please

JavaScript
var TestCal = {
    firstTextBoxID: "",
    secondTextBoxID: "",
    calTextBoxID: "",

    Initialize: function (submittedFirstTextBoxID, submittedSecondTextBoxID, submittedCalTextBox) {
        this.firstTextBoxID = submittedFirstTextBoxID;
        this.secondTextBoxID = submittedSecondTextBoxID;
        this.calTextBoxID = submittedCalTextBox;
    },

    Calculation: function () {

        var firstTextBoxValue = document.getElementById(this.firstTextBoxID).value;
        var secondTextBoxValue = document.getElementById(this.secondTextBoxID).value;
        var firstValue = parseInt(firstTextBoxValue != "" && firstTextBoxValue != "undefind" ? firstTextBoxValue : "0");
        var secondValue = parseInt(secondTextBoxValue != "" && secondTextBoxValue != "undefind" ? secondTextBoxValue : "0");
        var CalculatedValue = firstValue - secondValue;

        document.getElementById(this.calTextBoxID).value = CalculatedValue.toString();
    }
}



The aspx file is given below,

ASP.NET
<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">      
        <asp:TextBox  runat="server" ID="firstNumber" >
        <asp:TextBox runat="server" ID="secondNumber">
        <asp:TextBox  runat="server" ID="calculatedNumber" >  


And the .cs file is given below,
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 override void OnPreRender(EventArgs e)
    {
        base.OnPreRender(e);

        if (!Page.ClientScript.IsStartupScriptRegistered(this.GetType(), "TestCall"))
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "TestCall",
               "TestCal.Initialize('" + this.firstNumber.ClientID + "','" + this.secondNumber.ClientID + "','" + this.calculatedNumber.ClientID + "');", true);
        }

        string jsCalculation = "TestCal.Calculation()";
        firstNumber.Attributes.Add("OnKeyUp", jsCalculation);
        secondNumber.Attributes.Add("OnKeyUp", jsCalculation);
    }
}


Okay now run this and put value to fist two text boxes and the third one will display you the result. Here i didn't consider any validation. You need to do the validation in your own way.
 
Share this answer
 
v3
Comments
Md. Rashim Uddin 24-Aug-11 3:40am    
Please try with that i am doing it here for you and it is working man.

Thanks,
Rashim
walterhevedeich 24-Aug-11 4:49am    
Use PRE tags next time to make your code more readable. I have edited it this time for your benefit.
Md. Rashim Uddin 24-Aug-11 5:08am    
Thanks walterhevedeich
Md. Rashim Uddin 24-Aug-11 7:12am    
Please Vote it and mark it as Correct !!!

Thanks,
Rashim
Nimisha Mary John 25-Aug-11 0:55am    
hai Mr.Rashim,what is the problem of solution1?Is that Correct?
add event to your text box
C#
txtTwo.Attributes.Add("onchange", "Calculate();");


and add javascript to ur aspx page

JavaScript
<script type="text/javascript">

 function Calculate() {

 var txtOne = document.getElementById('txtValue1');
 var txtTwo = document.getElementById('txtValue2');
 var txtAns = document.getElementById('txtValueAns');
 txtAns.value=parseFloat(txtOne.value)-parseFloat(txtTwo.value);

       }

   </script>
 
Share this answer
 
v2
call following javascript function on "onblur" of Textbox3.
JavaScript
<script>
  function getResult()
  {
   document.getElementById("textbox3").value =  eval(document.getElementById("textbox1").value) - eval(document.getElementById("textbox2").value);

return false;
  }
</script>
 
Share this answer
 
XML
<script type="text/javascript" language="javascript">
      function SubstactData()
  {
    var txt1 = document.getElementById('<%=TextBox1.ClientID %>').value;
    var txt2 = document.getElementById('<%=TextBox2.ClientID %>').value;
    var txt3 = document.getElementById('<%=TextBox3.ClientID %>').value;

    document.getElementById('<%=TextBox3.ClientID %>').value = parseInt(txt1)+parseInt(txt2);
      }
</script>
 
Share this answer
 
Oh friends its not working....Is there is any need to enable 'Autopostback'?
 
Share this answer
 
Hello Am I enable 'Autopostback'?Its not working...
 
Share this answer
 
Aswathy
Am i change any other propery ?
The both code i written in aspx page but
still not working
 
Share this answer
 
Comments
walterhevedeich 24-Aug-11 4:48am    
STOP posting questions/comments as solution. If you have a question/comment, use the Have a Question or Comment? widget.
add event to your Text box one and two:
txtTwo.Attributes.Add("onchange", "Calculate("+txtOne.clientId+","+txtTwo.clientId+","+txtThree.clientId+");");


Now In java script Write Calculate Method
function Calculate(txtOneId,txtTwoId,txtThreeId)
{
 var txtOne = document.getElementById(txtOneId);
 var txtTwo = document.getElementById(txtTwoId);
 var txtAns = document.getElementById(txtThreeId);
  //Put checks here
 txtAns.value=parseFloat(txtOne.value)-parseFloat(txtTwo.value);
}
 
Share this answer
 
v2
ASP.NET
<asp:Textbox Id="txtVal2" runat="server" OnLeave= "Javascript:SubstactValue();" /> 


JavaScript
<script>
  function SubstactValue()
  {
    var txt1 = document.getElementById('txtVal1');
    var txt2 = document.getElementById('txtVal2');
    var txt3 = document.getElementById('txtVal3');
 
    txt3.value =  parseInt(txt1.value) - parseInt(txt2.value);
  }
</script>
 
Share this answer
 
v3
Comments
Nimisha Mary John 24-Aug-11 2:38am    
till not getting....
Am i change any other property?
Me written the first part of code in aspx page also second part.But its not working

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