473,407 Members | 2,306 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

the selected values in my checkboxlist are not adding correctly - ASP.NET

29
I am trying to add only my selected values but its only adding the first selected value in my list repeatedly to the amount of selected items. can someone help me with this?

for example, lets say I check off 2 in my list. than I check off three other boxes are inline after 2's box in my list not before. no matter what value is in those boxes I still get 8. 2+2+2+2


Expand|Select|Wrap|Line Numbers
  1. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  2.         lblAnswer.Text = ""
  3.         ListBox1.Items.Clear()
  4.         ''add up prices from list 
  5.         Dim total As Decimal
  6.         Dim i As Int32
  7.  
  8.         For i = 0 To CheckBoxList1.Items.Count - 1
  9.             If CheckBoxList1.Items(i).Selected Then
  10.                 total += Decimal.Parse(CDec(CheckBoxList1.SelectedValue.ToString))
  11.                 ListBox1.Items.Add(CheckBoxList1.SelectedValue)
  12.             End If
  13.  
  14.         Next
  15.         lblAnswer.Text = total
  16.  
  17.     End Sub
  18. End Class
Mar 13 '08 #1
1 1731
DrBunchman
979 Expert 512MB
Hi cluce,

Just so as you know - you've posted your question in a classic ASP forum. You'll get better advice if you post any future ASP.NET questions in the .NET forum. Having said that the solution to your problem is as follows:

You need to replace the two lines inside your For..Next loop which say this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. total += Decimal.Parse(CDec(CheckBoxList1.SelectedValue.ToString))
  3. ListBox1.Items.Add(CheckBoxList1.SelectedValue)
  4.  
To this:
Expand|Select|Wrap|Line Numbers
  1. total += Decimal.Parse(CDec(CheckBoxList1.Items(i).Value.ToString))
  2. ListBox1.Items.Add(CheckBoxList1.Items(i).Value.ToString
  3.  
The problem was that you weren't selecting a specific checkbox item (using the value of your loop counter, i) to take the value of.

Without specifiying which item in the CheckBoxList collection to use you were using the value of the first one it found which was checked.

I hope this helps,

Dr B
Mar 17 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Kumar | last post by:
Hi , I am using the article code shown in below url: http://www.codeproject.com/aspnet/xmlxsltransformer.asp?df=100&forumid=118504&select=1170328 This article is very cool feature. I...
0
by: Francois Verbeeck | last post by:
Dear UseNet readers, Does anyone have any idea on how to colorize selected checkbox in checkboxlist control ? I've quite a huge checkboxlist (approximatively one full screen) and, to improve...
4
by: krzysiek | last post by:
hello, i have several radiolists and checkboxlist that are generated dinamicly based on datasource. So frankly speaking i don't know names and number of that web controls cose they are always...
0
by: Boris | last post by:
When I dynamically create CheckButtonList, I add ListItem(s) to my CheckButtonList object chkList chkList.Items.Add(new ListItem("My Text", "My Value")); The resulting HTML doesn't contain...
5
by: Patrick.O.Ige | last post by:
I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList is in a Datalist) By doing "li.Selected = True" i can see all the checkBoxes are selected. But what i want is to be able...
1
by: sheenaa | last post by:
Hello friends, I m using ASP.NET with C# 2005 and SQL SERVER 2005 for the ASP.Net Website. I m using sqldatasource to connect and retrieve the data from database and then it displays the data...
1
by: momenee | last post by:
Problem: How to set the Selected property of items in a CheckBoxList in the EditItemTemplate of a FormView control that has been populated initially using an ObjectDataSource. I have a column in a...
1
by: cluce | last post by:
I have some prices listed in a checkboxlist that I am trying to add up. First I was going to add the mto a list box and add them up that way but I forgot how to do it. Its been awhile and I dont have...
1
by: cluce | last post by:
I am trying to add only my selected values but its only adding the first selected value in my list repeatedly to the amount of selected items. can someone help me with this? for example, lets say...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.