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

发布于 2024-11-01 19:32:59 字数 478 浏览 5 评论 0原文

嘿,我有一个单选按钮列表,并尝试将其中一个单选按钮设置为基于会话变量进行选择,但事实证明这是不可能的。

<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> 

即如何将 option2 设置为在代码后面选择?

Hey I have a radiobuttonlist and trying to set one of the radiobuttons to selected based on a session variable but proving impossible.

<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> 

I.e How can I set option2 to selected in code behind ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无语# 2024-11-08 19:32:59

我认为,最好的选择是使用 ListItemValue 属性,该属性在 RadioButtonList 中可用。

我必须指出 ListItem 是否NOT 有 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; 

The best option, in my opinion, is to use the Value property for the ListItem, which is available in the RadioButtonList.

I must remark that ListItem does NOT have an ID property.

So, in your case, to select the second element (option2) that would be:

// SelectedValue expects a string
radio1.SelectedValue = "1"; 

Alternatively, yet in very much the same vein you may supply an int to SelectedIndex.

// SelectedIndex expects an int, and are identified in the same order as they are added to the List starting with 0.
radio1.SelectedIndex = 1; 
傾旎 2024-11-08 19:32:59

您可以这样做:

radio1.SelectedIndex = 1;

但这是最简单的形式,随着 UI 的增长,很可能会出现问题。举例来说,如果团队成员在 option2 上方的 RadioButtonList插入一个项目,但不知道我们使用幻数 在代码隐藏中进行选择 - 现在应用程序选择了错误的索引!

也许您想研究使用 FindControl 为了通过名称确定实际需要的ListItem,并进行适当的选择。例如:

//omitting possible null reference checks...
var wantedOption = radio1.FindControl("option2").Selected = true;

You could do:

radio1.SelectedIndex = 1;

But this is the most simple form and would most likely become problematic as your UI grows. Say, for instance, if a team member inserts an item in the RadioButtonList above option2 but doesn't know we use magic numbers in code-behind to select - now the app selects the wrong index!

Maybe you want to look into using FindControl in order to determine the ListItem actually required, by name, and selecting appropriately. For instance:

//omitting possible null reference checks...
var wantedOption = radio1.FindControl("option2").Selected = true;
风吹短裙飘 2024-11-08 19:32:59

尝试这个选项:

radio1.Items.FindByValue("1").Selected = true;

Try this option:

radio1.Items.FindByValue("1").Selected = true;
脸赞 2024-11-08 19:32:59

我们可以按值更改项目,技巧如下:

radio1.ClearSelection();
radio1.Items.FindByValue("1").Selected = true;// 1 is the value of option2

We can change the item by value, here is the trick:

radio1.ClearSelection();
radio1.Items.FindByValue("1").Selected = true;// 1 is the value of option2
鹿港巷口少年归 2024-11-08 19:32:59
var rad_id = document.getElementById('<%=radio_btn_lst.ClientID %>');
var radio = rad_id.getElementsByTagName("input");
radio[0].checked = true;

//this for javascript in asp.net try this in .aspx page

// 如果您选择其他单选按钮,则将 [0] 增加到 [1] 或 [2],如下所示

var rad_id = document.getElementById('<%=radio_btn_lst.ClientID %>');
var radio = rad_id.getElementsByTagName("input");
radio[0].checked = true;

//this for javascript in asp.net try this in .aspx page

// if you select other radiobutton increase [0] to [1] or [2] like this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文