嘿,我有一个单选按钮列表,并尝试根据 session 变量将其中一个单选按钮设置为选择,但事实证明这是不可能的。
<asp:radiobuttonlist id="radio1" runat="server" AutoPostBack="True" OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:listitem id="option1" runat="server" value="All"/>
<asp:listitem id="option2" runat="server" value="1" />
<asp:listitem id="option3" runat="server" value="2" />
</asp:radiobuttonlist>
即如何将选项2设置为在后面的代码中选择?
最佳答案
在我看来,最好的选择是使用 ListItem
的 Value
属性,该属性在 RadioButtonList
中可用。
我必须指出ListItem
不有 ID 属性。
因此,在您的情况下,选择第二个元素(选项2):
// SelectedValue expects a string
radio1.SelectedValue = "1";
或者,以同样的方式,您可以向 SelectedIndex 提供一个 int。
// SelectedIndex expects an int, and are identified in the same order as they are added to the List starting with 0.
radio1.SelectedIndex = 1;
关于asp.net - 设置从代码隐藏中选择的单选按钮列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5662113/