Solved

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

Posted on 2006-06-19
4
223 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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