473,803 Members | 3,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Javascript calculating total in gridview

parshupooja
159 New Member
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)+(colum n4-column3)
Here is javascript i am using. When i use this it just calculates for first row of gridview.
Expand|Select|Wrap|Line Numbers
  1. <script language =javascript type="text/javascript">
  2.                        function Total()
  3.                                {
  4.  
  5.                                 var int1=parseInt(document.getElementById("ctl00_contentPlaceHolder_GridView1_ctl02_TextBox1").value);
  6.                                 var int2 = parseInt(document.getElementById("ctl00_contentPlaceHolder_GridView1_ctl02_TextBox2").value);
  7.                                 var int3 = parseInt(document.getElementById("ctl00_contentPlaceHolder_GridView1_ctl02_TextBox3").value);
  8.                                 var int4 = parseInt(document.getElementById("ctl00_contentPlaceHolder_GridView1_ctl02_TextBox4").value);
  9.                                 var int5 = (int2-int1+int4-int3);
  10.                                 document.getElementById("ctl00_contentPlaceHolder_GridView1_ctl02_lblTotalHr").innerText = int5;
  11.  }
  12. </script>
  13.  
i have tried using document.getEle mentById("<%=Te xtBox1.ClientID %>") and

document.getEle mentById("<%=Gr idView1.TextBox 1.ClientID%>") but throws error

plz help
Sep 20 '07 #1
9 11296
gits
5,390 Recognized Expert Moderator Expert
hi ...

as far as can see your script is doing what you want from it :) ... in case you have 14 rows you have to loop through you rows and call the function for every row you have ... and then you have to add all row-totals?

kind regards
Sep 20 '07 #2
parshupooja
159 New Member
your are right i need to loop thr it,but since only bolded part is changing for each control, i don't know how to do it. could show me little snippet or refer meto the article

ctl00_contentPl aceHolder_GridV iew1_ctl02_TextBox1
hi ...

as far as can see your script is doing what you want from it :) ... in case you have 14 rows you have to loop through you rows and call the function for every row you have ... and then you have to add all row-totals?

kind regards
Sep 20 '07 #3
gits
5,390 Recognized Expert Moderator Expert
that is a little bit tricky and depends on how this changes ... you may use something like this:

Expand|Select|Wrap|Line Numbers
  1. var prefix = 'ctl00_contentPlaceHolder_GridView1_';
  2.  
  3. // you may pass this as a param to your function
  4. // here i use: cp = 'ctl02'
  5. var changing_part = cp;
  6.  
  7. var suffix = '_TextBox1';
  8.  
  9. var id = prefix + changing_part + suffix;
  10.  
  11. var node = document.getElementById(id);
i assume that you know how to loop through your lines ... so you call your function with the cp-param each time with a new row.

kind regards
Sep 20 '07 #4
parshupooja
159 New Member
Hello
Thank You for help but when i just test it and it shows msg null object. any clue?
Expand|Select|Wrap|Line Numbers
  1. var msg = "";
  2.                                    for (var x = 1; x <= 10; x++)
  3.                                   {
  4.                                             var prefix = "ctl00_contentPlaceHolder_GridView1_ctl0";
  5.                                          var changing_part = x;
  6.                                           var suffix = "_TextBox1";
  7.                                            var id = prefix + changing_part + suffix;
  8.                                            var node = document.getElementById(id);
  9.                                           javascript:window.alert(node);
  10.  
  11.  
that is a little bit tricky and depends on how this changes ... you may use something like this:

Expand|Select|Wrap|Line Numbers
  1. var prefix = 'ctl00_contentPlaceHolder_GridView1_';
  2.  
  3. // you may pass this as a param to your function
  4. // here i use: cp = 'ctl02'
  5. var changing_part = cp;
  6.  
  7. var suffix = '_TextBox1';
  8.  
  9. var id = prefix + changing_part + suffix;
  10.  
  11. var node = document.getElementById(id);
i assume that you know how to loop through your lines ... so you call your function with the cp-param each time with a new row.

kind regards
Sep 20 '07 #5
gits
5,390 Recognized Expert Moderator Expert
i think your prefix is wrong .... it seems to contain the variable part?

kind regards
Sep 20 '07 #6
parshupooja
159 New Member
actually only value after ctl0 is increasing by one. so i am considering it as a part of prefix

so i thought x would be 1,2,........... .n and so on

string should be
ctl00_contentPl aceHolder_GridV iew1_ctlx_TextBox1


Thanks
i think your prefix is wrong .... it seems to contain the variable part?

kind regards
Sep 20 '07 #7
gits
5,390 Recognized Expert Moderator Expert
hmmm ... i see ... when do you call the loop? ... it has to be after! the page is fully loaded so that the dom is ready to use ...

kind regards
Sep 21 '07 #8
parshupooja
159 New Member
Loop is in the script tag of of aspx page and i am calling this function at
Onchange event of Textboxes, thats means page is fully loaded already , it triggers only when I change value on Textboxes
Expand|Select|Wrap|Line Numbers
  1. <script language =javascript type="text/javascript">
  2.                         function Total(){
  3.  for (var x = 1; x <= 10; x++)
  4.                                     {
  5.                                          var prefix = "ctl00_contentPlaceHolder_GridView1_ctl0";
  6.                                         var changing_part = x;
  7.                                           var suffix = "_TextBox1";
  8.                                           var id = prefix + changing_part + suffix;
  9.                                           var node = document.getElementById(id);
  10. }
  11. }
  12. </script>
  13.  
gridview
Expand|Select|Wrap|Line Numbers
  1.  <asp:TemplateField HeaderText="Out">
  2.                     <ItemTemplate>
  3.                         <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("MorningOut") %>' Width="50px" onChange="Total()"></asp:TextBox>
  4.                     </ItemTemplate>
  5.                 </asp:TemplateField>
  6.  
  7.  
hmmm ... i see ... when do you call the loop? ... it has to be after! the page is fully loaded so that the dom is ready to use ...

kind regards
Sep 21 '07 #9
gits
5,390 Recognized Expert Moderator Expert
hi ...

hmmm ... could you please post the html-code? ... when the page is rendered by the browser goto view source and post the html that relates to our problem ... so that we may have a closer look ...

kind regards
Sep 23 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

53
5749
by: Cardman | last post by:
Greetings, I am trying to solve a problem that has been inflicting my self created Order Forms for a long time, where the problem is that as I cannot reproduce this error myself, then it is difficult to know what is going on. One of these Order Forms you can see here... http://www.cardman.co.uk/orderform.php3
0
2007
by: hlam | last post by:
Help - Calculating the total of a column in a data grid -- when data grid is part of Master-Detail set-up I have setup a Master-Detail form using Visual Studio.Net. A ListBox is the (Master) and contains a list of dates. The data grid (Details portion) contains the details for a selected date (namely, a list of work codes and hours spent). I am trying to total the number of hours in the datagrid
3
16440
by: Amelyan | last post by:
I need to get the total number of items/records returned into GridView. If I just do myGridView.Rows.Count, then it just returns me the total number of items on the page. But if I have, 10 pages (10 records per page) and 98 records total, Rows.Count will return me 10 on the first 9 pages, and 8, on the last page. But, I need to somehow get access to total record count which is 98. What is the proper/easy way to accomplish that?
0
1536
by: jimmy6509 | last post by:
Hello all, asp.net 2.0, VB. Have a simple question that I hope someone can help me with. gridview -> queries for a filtered recordset, example purchase orders attributed to a specific customer. have figured out how to total in the gridview footer.
10
3790
by: Lisa | last post by:
In translating the formula for calculating lottery odds for various conditions into a Visual Basic Program, I have apparently missed something in that I get errors in the part of the calculation where the number of ways of failure (pFal) is calculated Both errors happen in the code section x1 = Draw - MatchesReq x2 = Field - Selections For Counter = 1 To (Draw - MatchesReq - 1) x1 = x1 * (Draw - MatchesReq - Counter)
0
913
parshupooja
by: parshupooja | last post by:
Hi All, I have gridview with following columns date--morningin--morningout--afetrnoonin--afetrnoonout I want to create another 6th coulmn which shd display total hours(morningout-morningin)+(afternoonout-afetrnoonin) Can anyone help me with this? I am fed up of trying.
0
1188
parshupooja
by: parshupooja | last post by:
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">
1
2216
by: r786amesh | last post by:
Hello Everybody, My question is I want to display Nested gridview footer total by using javascript. its very urgent pls help me... thnx ramesh
13
1855
by: DDragon | last post by:
ok here is the problem, i have to forms which have values i wish to be added together i can add together the values in one form all right but im having problems with adding the values of the other form to it... ill post the code for you to look at and try to explaine in detail what im trying to do. here is the code (HTML and Javascript so you get the whole picture): Javascript: var gen, lights; var lammount;
4
4037
by: sumit kale | last post by:
Hi, Can somebody help me resolve my problem ? I am getting error when calculating total using unbound textfiled in subform. I have a main form called purchase_register_master and a subform called purchase_register_details, In sub form i have a unbound textfield called txt_Total_Amount where in i am calculating total amount for different items purchased by giving following code in control source. ContolSource: =Sum() In my main form i...
0
10546
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10292
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10068
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9121
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6841
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4275
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3796
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2970
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.