Solved

Javascript to sum the total value from a gridview dropdownlist

Posted on 2009-05-18
4
738 Views
Last Modified: 2013-12-17
I have a gridview column with dropdownlist. When a user selects a value from the dropdown, a textbox outside the gridview should show the selected value, and for each selection in different row, the value should be added up and display the total amount in the textbox .

For example

!st dropdownlist value =3155
2nd dropdownlist value=1900

then the text box should show=$5000

Right now i have the javascript code to show the dropdownlist selected value. could anybody please help me on this to show the total amount .

thanks

  function showData(dropdown)
     {
        //get dropdownlist
        var myindex  = dropdown.selectedIndex;
        var SelValue = dropdown.options[myindex].value;
       var TextBox1Id = '<%=txtmanagerprice.ClientID %>';
   
     var box=document.getElementById(TextBox1Id);
     
     box.value=  SelValue;
  }
    </script>


Gridview Dropdown

 <asp:TemplateField HeaderText="Manager Approved Equip">
                                <ItemTemplate>
                                    <asp:DropDownList ID="drpequip" runat="server" DataSource='<%#GetReportGroupList() %>'
                                        onchange="showData(this)" DataTextField="Equipment" DataValueField="Price" Width="100%" />
                                </ItemTemplate>
                                <ItemStyle Width="150px" />
                            </asp:TemplateField>
0
Comment
Question by:gladstonesheeba
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:channa_m
ID: 24416358
Replace the line in you code that says
      box.value=  SelValue;

with the following code
     if (box.value != ""){
        box.value=  eval(box.value) + SelValue;
     } 
     else 
     {
       box.value=   SelValue;
     }

Open in new window

0
 

Author Comment

by:gladstonesheeba
ID: 24420975
For each dropdown selection , its showing the value like this

30423042461430424614

it should be for each selection , it should add the value , not concatenation.

3042
3042
4614
3042
4614

Total amount should display in the text box is  $18,354

i don't know how to do this in javascript.
0
 
LVL 3

Accepted Solution

by:
channa_m earned 500 total points
ID: 24421768
Convert the value to Int before adding see below

if (box.value != ""){
        box.value=  parseInt(eval(box.value)) + parseInt(SelValue);
     } 
     else 
     {
       box.value=   SelValue;
     }

Open in new window

0
 

Author Closing Comment

by:gladstonesheeba
ID: 31582709
Excellent, thank you so much.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question