Solved

Javascript to sum the total value from a gridview dropdownlist

Posted on 2009-05-18
4
744 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

615 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