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
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,
<%@ 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,
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.