Solved

Javascript to sum the total value from a gridview dropdownlist

Posted on 2009-05-18
4
734 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Excellent, thank you so much.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
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…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now