Asp Radiobuttonlist. Selected Issues

I have a radio button list inside of an AJAX panel. Here is the radio button list:

<asp:RadioButtonList ID="RadioButtonList" runat="server" TextAlign="Right" AutoPostBack="true">
     <asp:ListItem Text="Option3" Value="Option3" Selected="True" />
     <asp:ListItem Text="Option1" Value="Option1" />
     <asp:ListItem Text="Option2" Value="Option2" />
</asp:RadioButtonList>

I have a function that loads the value based on saved settings. It looks similar to this:

string selectedOption = savedRecord.RadioButtonListValue.ToString();
RadioButtonList.Items.FindByValue(selectedOption).Selected = true;

It only seems to correctly load the value if I haven't changed the selected option.

  • If I load the page, the load the settings it will correctly set to the saved option.
  • If I load the page, change the option, then load the settings it will not change the option.

I have tried with AutoPostBack set to true and false and it doesn't seem to change the result. Any ideas?

I have been able to hard code a value and it seems to always load correctly:

//Working
RadioButtonList.Items.FindByValue("Option1").Selected = true;

//Not-working
string selectedOption = savedRecord.RadioButtonListValue.ToString(); //"Option1"
RadioButtonList.Items.FindByValue(selectedOption).Selected = true;

Here are the combinations that I have come up with.

4

1 Answer

Please try the ASPX code will look something like this:

The ASPX code will look something like this:

 <asp:RadioButtonList ID="rblist1" runat="server">

    <asp:ListItem Text ="Item1" Value="1" />
    <asp:ListItem Text ="Item2" Value="2" />
    <asp:ListItem Text ="Item3" Value="3" />
    <asp:ListItem Text ="Item4" Value="4" />

    </asp:RadioButtonList>

    <asp:Button ID="btn1" runat="server" OnClick="Button1_Click" Text="select value" />

And the code behind:

protected void Button1_Click(object sender, EventArgs e)
        {
            string selectedValue = rblist1.SelectedValue;
            Response.Write(selectedValue);
        }
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Robert Thorne

Robert Thorne

Automotive & Future Transportation Editor

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.

Share this article
Twitter Facebook Pinterest