Search code examples
asp.netradiobuttonlistselectedindexchangedselectedindex

For the asp.net radiobuttonlist: getting a list item by FindByValue and setting the Selected attribute not working?


Probably a basic question but I have the following code:

ListItem l = radiolist.Items.FindByValue(mediaTypeID.ToString());
if (l != null)
  l.Selected = true;
else
  radiolist.SelectedIndex = 0;

handleMediaTypeChanged();

In the above code, I can successfully retrieve the correct item from the radiobuttonlist, however setting the item to be Selected is not working. The SelectedIndexChanged event does not fire, and when I call it manually using handleMediaTypeChanged() the radiobuttonlist does not reflect a changed index. What is the proper way if this isn't it?


Solution

  • As far as I know, the SelectedIndexChanged event will only fire when you've physically changed the index from the UI.

    You can try something like this:

    ListItem l = radiolist.Items.FindByValue(mediaTypeID.ToString());
    
    radioList.ClearSelection();
    if (l != null)
        l.Selected = true;