Solved

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

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

20 Experts available now in Live!

Get 1:1 Help Now