RadioButtonList does not allow you to select a ListItem other than the first one that has a specifie

N

Nathan Sokalski

I have a RadioButtonList with more than one ListItem that has a certain
Value. If I select a ListItem with that Value, the one that is selected is
the first ListItem with that Value. For example, if I had the following
ListItems:

<asp:ListItem Text="Boardgames" Value="12.00"/>
<asp:ListItem Text="Stuffed Animals" Value="8.00"/>
<asp:ListItem Text="Pens" Value="3.00"/>
<asp:ListItem Text="Mouse Pad" Value="2.00"/>
<asp:ListItem Text="Screwdriver" Value="12.00"/>
<asp:ListItem Text="Notecards" Value="3.00"/>

If someone were to select Notecards, the ListItem that would end up being
selected would be Pens, and if Screwdriver was selected, it would actually
select Boardgames. This problem only occurs when ListItems have the same
value. I am using AJAX when this is occurring. Does anybody have any
suggestions for an easy way to avoid this problem without changing the
Values? Thanks.
 
N

Nathan Sokalski

I have finally found a simple solution, here is the code:

If Not String.IsNullOrEmpty(Me.Request.Form("__EVENTTARGET")) Then 'Make
sure there is a value; there is no value when the page is first launched
Dim eventtarget As String = Me.Request.Form("__EVENTTARGET").Replace("$",
"_") 'Replace the $'s in the returned value with underscores; underscores
are what is returned by the ClientID property
If eventtarget.StartsWith(Me.rblChoices.ClientID) Then
Me.rblChoices.SelectedIndex =
CInt(eventtarget.Substring(eventtarget.LastIndexOf("_") + 1)) 'Parse the
string to retrieve the index
End If

This code should be placed as early as possible in the Page lifecycle. I
placed it in the Page's Load event, since the properties are not generally
used or available before then. I have not extensively tested this for
compatibility for all scenarios. As you can see from my original posting, my
original scenario had static ListItems, so you may want to do some testing
of your own if you are dynamically populating the RadioButtonList.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top