asma
asma

Reputation: 2825

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.

Upvotes: 1

Views: 22263

Answers (4)

e-MEE
e-MEE

Reputation: 508

You can't set the selected radiobutton with .SelectedValue, only with .SelectedIndex.

Check MSDN (at SelectedValue it says "Gets the value", at SelectedIndex is says "Gets or sets")

Upvotes: 2

balexandre
balexandre

Reputation: 75073

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

Upvotes: 4

mayur Rathod
mayur Rathod

Reputation: 1404

hello friend there is something another problem in your code because in my side this is running fine.

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

can u cehck that clientMedicationSchedule.glTypeId.ToString() this contain value or not. and on page load u put selection code on if (!IsPostBack){} block.

Upvotes: 0

Pankaj Agarwal
Pankaj Agarwal

Reputation: 11311

I think problem in !IsPostBack.

if (!IsPostBack)
{
string str = rblMedicationTime.SelectedValue;
}

First you should check !IspostBack

Upvotes: 1

Related Questions