开发者

Why is my RadioButtonList selectedValue empty?

开发者 https://www.devze.com 2023-03-31 11:04 出处:网络
I have a RadioButtonList: &开发者_运维百科lt;asp:RadioButtonList ID=\"rblMedicationTime\" runat=\"server\"onselectedindexchanged=\"rblMedicationTime_SelectedIndexChanged\"DataSourceID=\"dtsMedication

I have a RadioButtonList:

&开发者_运维百科lt;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.


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


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")


I think problem in !IsPostBack.

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

First you should check !IspostBack


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消