Solved

Display array values in RadioButtonList

Posted on 2011-02-23
12
489 Views
Last Modified: 2012-08-13
Hello Experts

I have a radiobuttonlist on my aspx form.  In the code-behind I populate this list using a simple array:
accessarray = strUseraccesscorp.Split(",")
rblStoreChoice.DataSource = accessarray
rblStoreChoice.DataBind()

This displays the string values properly.  An example of the data being displayed:
1,2,3,4

Once split, of course, it looks like this on the radio button list:
1
2
3
4

Instead of showing the numeric values I'd like to display something more readable.  Is there some way I can loop through these array values and add a DataTextField value to the RBL of something easier to read?
Example:
If value = 1 then A elseif value = 2 then B elseif value = 3 then C and so on...

How can I show a better radio button list yet still use the numeric values when a selection is made?
0
Comment
Question by:jay-are
  • 6
  • 2
  • 2
  • +1
12 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 34962745
Use Arraylist.
0
 
LVL 20

Assisted Solution

by:informaniac
informaniac earned 100 total points
ID: 34962795
Use Arraylist.
0
 

Author Comment

by:jay-are
ID: 34963903
I'm not familiar with ArrayList.  I just looked it over and I'm not sure how it will solve my problem.  I won't always know the values I'm using to populate my current array.  There are 15 different possible values.  Sometimes the array will have 1 value, sometimes it could have 10.  My interest is in hard-coding a real name for these values that I can use to display on my radio button list, while keeping the original values as the DataValueField so it can be used when a selection is made.  I thought maybe a loop would work in this case but I'm unable to write one on my own.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 12

Assisted Solution

by:omegaomega
omegaomega earned 200 total points
ID: 34964089
Hello, jay-are,

I'm a little out of my usual territory here, so if this is off-base, please forgive.  But can't you just loop through the radio button list after it is created and set the text of each to anything that you want.  For example:

        Dim strUseraccesscorp As String = "1,2,3,4"
        Dim accessarray As String() = strUseraccesscorp.Split(","c)
        rblStoreChoice.DataSource = accessarray
        rblStoreChoice.DataBind()

        For intButton As Integer = 0 To rblStoreChoice.Items.Count - 1
            rblStoreChoice.Items(intButton).Text = "This is button # " & intButton
        Next intButton

Cheers,
Randy
0
 

Author Comment

by:jay-are
ID: 34964230
Hi Randy

Thanks for your response.  I think I need something along the lines of this:

accessarray = strUseraccesscorp.Split(",")
            rblStoreChoice.DataSource = accessarray
            For Each RadioButton In rblStoreChoice.Items
                If rblStoreChoice.DataValueField = "123456789" Then
                    rblStoreChoice.DataTextField = "Something more readable"
                End If
            Next
            rblStoreChoice.DataBind()

I just can't make this work.
0
 
LVL 11

Assisted Solution

by:srikanthreddyn143
srikanthreddyn143 earned 200 total points
ID: 34964256
Try something like this:

Dim htValues As New Hashtable
        htValues.Add("1", "A")
        htValues.Add("2", "B")
        For Each oItem As DictionaryEntry In htValues
            Dim oListItem As New System.Web.UI.WebControls.ListItem
            oListItem.Text = oItem.Value
            oListItem.Value = oItem.Key
            RadioButtonList1.Items.Add(oListItem)
        Next
0
 
LVL 11

Expert Comment

by:srikanthreddyn143
ID: 34964273
You need to populate hashtable with keys as data in your array and value with the text you want.
0
 

Author Comment

by:jay-are
ID: 34964548
How do I add the existing array to the hashtable and then, based on the array value, assign text for it?  Like I mentioned above, I won't always know the numeric values of this array, I need to write a statement that will convert all possible numeric values to text values so the list looks better.
0
 
LVL 12

Expert Comment

by:omegaomega
ID: 34964626
Hello, jay-are,

I think that (in typical use) DataValueField and DataTextField would be the field names in the bound DataSource.  In your case, the bound data has only one column and no field names, so these values will be empty strings.  Also, these values are the same for all radio buttons in the set, so there is no point in including them in a loop through the radio buttons.

You can get the "numeric" value of the user's selection by using rblStoreChoice.SelectedValue.  (I put "numeric" in quotes here, because you are actually assigning strings to the values.  The strings just happen to represent numeric values.  If you want an actual integer value, you coudl use rblStoreChoice.SelectedIndex (or rblStoreChoice.SelectedIndex + 1 if you want to start counting at one).

Maybe we need to get a better image of the "big picture" of what you are trying to accomplish.

Cheers,
Randy
 
0
 

Author Comment

by:jay-are
ID: 34964681
Yeah I'm horrible at explaining what I actually need.

I want to present my users with a list of names.  They can pick one name only and then perform an action.  This action will need to know the value of what they selected.  In my existing database I have these values stored in 2 different ways.  Examples:
db values
1234

or
1234,4321,5678

There are 15 possibilities.  These values in the db represent a word that I want to show on the list.  So I need a way of getting the values in the db and presenting them as something that is easier to understand.  I was hoping to just hard-code the actual text field on the list based on an if/then statement since I don't store them anywhere in the db.  The action will need to know the actual numeric choice they made (example: 1234  or  5678).  

I have the radio button list populated already with the actual data from the db, now I just need a way of changing what the list is displaying to something more user friendly.  Hopefully this makes sense.
0
 

Accepted Solution

by:
jay-are earned 0 total points
ID: 34964779
Ok this seems to work:

accessarray = strUseraccesscorp.Split(",")
            rblStoreChoice.DataSource = accessarray
            rblStoreChoice.DataBind()
            Dim rblItem As New ListItem
            For Each rblItem In rblStoreChoice.Items
                If rblItem.Text = "1234" Then
                    rblItem.Text = "Truck"
                ElseIf rblItem.Text = "5678" Then
                    rblItem.Text = "Car"
                End If
            Next

The radio button list shows the names like truck and car.  When I select one of the items and perform a button click I get the numeric value for the selection like "1234" which is exactly what I needed.
0
 

Author Closing Comment

by:jay-are
ID: 34995507
not a pretty solution, but it works
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
"Emulate" TAB key when press Enter Key 3 47
Optimize the query 5 43
IEnumerable<T> to a List<T> 8 38
Toolbar to format all windows forms controls at design time needed 5 15
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 …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

837 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