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

Discussion in 'ASP .Net Web Controls' started by Nathan Sokalski, Nov 27, 2007.

  1. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/
     
    Nathan Sokalski, Nov 27, 2007
    #1
    1. Advertising

  2. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "Nathan Sokalski" <> wrote in message
    news:...
    >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.
    > --
    > Nathan Sokalski
    >
    > http://www.nathansokalski.com/
    >
     
    Nathan Sokalski, Nov 28, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mad Scientist Jr
    Replies:
    2
    Views:
    479
    Mad Scientist Jr
    Jun 29, 2004
  2. Karl
    Replies:
    0
    Views:
    801
  3. Jim in Arizona
    Replies:
    7
    Views:
    951
    Badass Scotsman
    Nov 22, 2006
  4. Nathan Sokalski
    Replies:
    0
    Views:
    836
    Nathan Sokalski
    Oct 25, 2007
  5. Nathan Sokalski
    Replies:
    1
    Views:
    814
    Nathan Sokalski
    Nov 28, 2007
Loading...

Share This Page