Solved

How to add together the selectedvalues from 2 radiobuttonlist controls and show the result in a textbox?

Posted on 2006-06-19
4
230 Views
Last Modified: 2006-11-18
If I have these radiobuttonlist controls on my form:
radNumber1 (options are 0, 1, 2, 3, 4, 5)
radNumber2 (options are 0, 1, 2, 3, 4, 5)

and this textbox on my form:
txtAnswer

how can I add together whatever was selected for radNumber1 and radNumber2, and show the result in txtAnswer?

A couple things in particular:
- I need to only show the result when they have picked something for BOTH radNumber1 and radNumber2 (meaning, neither can have no selection)
- If they picked something first, and the sum showed up in the textbox already, but then they change their mind and pick something else for either one or both of the radiobuttonlists, I need to txtAnswer sum to automatically recalculate and show the correct sum for the new selections.

Thank you!!!
0
Comment
Question by:sah18
  • 2
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
davehunt00 earned 250 total points
ID: 16939261
Try this:

The nice thing about your radio button lists is your values correspond to the index values, so it make the math simple.

Create your two radiobuttonlists (I named mine rbl1 and rbl2) with the Selected values being 0 thru 5 - with the zero in the first position so it has an index of 0 as well. For each radio button list, set the Autopostback to true.

An unselected radiobuttonlist has an index of -1, so you can use that to tell whether something has been selected. Putting this in the IfThen prevents the addition from firing until the user selects something from both lists. Once they do, the autopostback keeps the text field updated.

In your page_load, use this:

        If (rbl1.SelectedIndex <> -1) And (rbl2.SelectedIndex <> -1) Then
            txtAnswer.Text = rbl1.SelectedIndex + rbl2.SelectedIndex
        End If


Hope that works for you.
Dave
0
 
LVL 9

Author Comment

by:sah18
ID: 16942296
Dave,
This is working!!  Thank you so much!  I'd like to tweak this slightly, and instead of just summing, I'd like to average the values (it will be more than just the two lists I started with), but when I display the result in the textbox, I'd like to format it, so that it shows the amount out to 2 decimal places (ex: 4.20).  Do you know how I can format the number so that it shows up like this in that same textbox?  Thank you again for all your help!!
0
 
LVL 4

Expert Comment

by:davehunt00
ID: 16943890
Sure, just do this.

You can add on as many radio button lists as you need in the If statement.

Then if you add the sums together and divide by however many lists you have (I had 3), that amount can be converted to a string with the Fixed-2-Decimals format ("F2").  There are a lot of various formats, for example "C" will give you 2-decimal currency if you need it.

        If (rbl1.SelectedIndex <> -1) And (rbl2.SelectedIndex <> -1) And (rbl3.SelectedIndex <> -1) Then
            txtAnswer.Text = ((rbl1.SelectedIndex + rbl2.SelectedIndex + rbl3.SelectedIndex) / 3).ToString("F2")
        End If

All the best.
Dave
0
 
LVL 9

Author Comment

by:sah18
ID: 16943966
outstanding!  thanks again!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

830 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