Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-06-19
4
Medium Priority
?
251 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 1000 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

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…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

824 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