Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Javascript to sum the total value from a gridview dropdownlist

Posted on 2009-05-18
4
Medium Priority
?
746 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 2000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
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

782 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