Solved

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

Posted on 2006-06-19
4
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

617 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