Search code examples
c#asp.netradiobuttonlist

Check radio button in RadioButtonList based on value read from database


I have a RadioButtonList and I would like to select a radio button in this list based on a value read from a database. Once I read the value from the database, how do I then make the radio button in the list become selected?


Solution

  • You can simply assign the value via RadioButtonList.SelectedValue = reader.value; as long as you know that the value is in the list (if it's not in the list, then you will get an exception when that line executes).

    Since it sounds like you do not know for sure that reader.value will be one of the options in the RadioButtonList, so you will need to check that first.

    if(RadioButtonList.Items.FindByValue(reader.value) != null) {
        RadioButtonList.SelectedValue = reader.value;
    }
    

    Alternatively, you could handle the exception via a try/catch.