简体   繁体   English

ASP RadioButtonList .Selected问题

[英]ASP RadioButtonList .Selected Issues

I have a radio button list inside of an AJAX panel. 我在AJAX面板中有一个单选按钮列表。 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. 我尝试将AutoPostBack设置为true和false,并且它似乎并没有改变结果。 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: 请尝试使用ASPX代码,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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