asp.net - 设置从代码隐藏中选择的单选按钮列表

标签 asp.net radiobuttonlist

嘿,我有一个单选按钮列表,并尝试根据 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设置为在后面的代码中选择?

最佳答案

在我看来,最好的选择是使用 ListItemValue 属性,该属性在 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/

相关文章:

javascript - 用于更改图像的单选按钮(Javascript/HTML)

javascript - 如何从单选按钮列表中更改已选择的单选按钮的颜色

android - 选择选项后如何关闭我的 inflater 菜单

c# - 代码花费大部分时间的地方

c# - Global.asax 文件中的事件监听器脚本需要持续运行

javascript - jQuery 验证 - 如何显示错误时的默认标注?

c# - 使用 onServerClick 获取 href 值

c# - ASP.NET Core中如何进行异常处理?

javascript - Yii2:如何禁用单选按钮

html - 如何将属性添加到输入标签 asp.net 内的 RadioButtonList 项目