简体   繁体   中英

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.

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); } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM