Search code examples
asp.netradio-buttonradiobuttonlist

Why is my RadioButtonList selectedValue empty?


I have a RadioButtonList:

<asp:RadioButtonList ID="rblMedicationTime" runat="server"  onselectedindexchanged="rblMedicationTime_SelectedIndexChanged"  DataSourceID="dtsMedicationTime" DataTextField="LookupItem" DataValueField="Id"  AutoPostBack="true"></asp:RadioButtonList>

On page load, I want to select a radio button from the list and set its value, for which I have written this line of code:

rblMedicationTime.SelectedValue = clientMedicationSchedule.glTypeId.ToString();

The RadioButtonList is populating successfully, but the value is unable to be selected.

rblMedicationTime.SelectedValue is always "" when I debug the code.


Solution

  • You just need to use

    string myValue = myRadioButtonList.SelectedItem.Value
    

    The property object myRadioButtonList.SelectedItem contains all values from the selected item of a Radio Button list or a DropDown list


    to set the value programmatically all you have to do is:

    myRadioButtonList.SelectedIndex = 0;
    

    You can see that you have several ways to Get but only one to Set:

    • myRadioButtonList.SelectedIndex --> Gets or Sets
    • myRadioButtonList.SelectedValue --> Gets
    • myRadioButtonList.SelectedItem --> Gets