Solved

Javascript to sum the total value from a gridview dropdownlist

Posted on 2009-05-18
4
742 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

820 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