/    Sign up×
Community /Pin to ProfileBookmark

calculating filed in gridview with javascript

Hi
I am using asp.net C# and javascript.
I have a gridview with 5 columns and 14 row
column1–column2–column3–column4–column5

Column1 to column4 is textboxes wheresa column5 is label
I am trying to calculate column 5 which is (column2-column1)+(column4-column3)
Here is javascript i am using. When i use this it just calculates for first row of gridview.

<script language =javascript type=”text/javascript”>

function Total()

{

var int1=parseInt(document.getElementById(“ctl00_contentPlaceHolder_GridView1_ctl02_TextBox1”).value);
var int2 = parseInt(document.getElementById(“ctl00_contentPlaceHolder_GridView1_ctl02_TextBox2”).value);

var int3 = parseInt(document.getElementById(“ctl00_contentPlaceHolder_GridView1_ctl02_TextBox3”).value);

var int4 = parseInt(document.getElementById(“ctl00_contentPlaceHolder_GridView1_ctl02_TextBox4”).value);

var int5 = (int2-int1+int4-int3);

document.getElementById(“ctl00_contentPlaceHolder_GridView1_ctl02_lblTotalH r”).innerText = int5;

}

</script>

i have tried using document.getElementById(“<%=TextBox1.ClientID%>”) and

document.getElementById(“<%=GridView1.TextBox1.ClientID%>”) but throws error

plz help

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@NatdripSep 21.2007 — this should get you going

[CODE]<!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>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>

<script type="text/javascript">
function Total(){

var int1 = parseInt(document.getElementById("ctl00_contentPlaceHolder_GridView1_ctl02_TextBox1").value);
var int2 = parseInt(document.getElementById("ctl00_contentPlaceHolder_GridView1_ctl02_TextBox2").value);
var int3 = parseInt(document.getElementById("ctl00_contentPlaceHolder_GridView1_ctl02_TextBox3").value);
var int4 = parseInt(document.getElementById("ctl00_contentPlaceHolder_GridView1_ctl02_TextBox4").value);

var int5 = 1*(int2-int1+int4-int3);

document.getElementById("ctl00_contentPlaceHolder_GridView1_ctl02_lblTotalH r").value = int5;
document.getElementById("ctl00_contentPlaceHolder_GridView1_ctl02_lblTotalH r2").innerHTML = int5;
}
</script>

</head>

<body>
<a href="javascript:;" onclick="Total()">add me box arrrr!!!</a><br />
<input id="ctl00_contentPlaceHolder_GridView1_ctl02_TextBox1" type="text" /> 1 <br />
<input id="ctl00_contentPlaceHolder_GridView1_ctl02_TextBox2" type="text" /> 2 <br />
<input id="ctl00_contentPlaceHolder_GridView1_ctl02_TextBox3" type="text" /> 3 <br />
<input id="ctl00_contentPlaceHolder_GridView1_ctl02_TextBox4" type="text" /> 4 <br />
<input id="ctl00_contentPlaceHolder_GridView1_ctl02_lblTotalH r" type="text" /> total
<div id="ctl00_contentPlaceHolder_GridView1_ctl02_lblTotalH r2"></div>
</body>
</html>[/CODE]
×

Success!

Help @PROPOO spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...