RadioButton和RadioButtonList有什么区别?

3
我需要知道RadioButton和RadioButtonList之间的区别,以及在决定使用哪种控件时需要遵循哪些指南?
我已经进行了研究,并决定在这里发布我的发现,以帮助说明我所发现的差异:
我学到了:
RadioButton
用于一次只显示一个单选按钮。可能需要设置group属性来将多个RadioButton控件关联成一个组。
RadioButtonList
用于显示一组RadioButton控件,自动提供group属性,将所有包含的RadioButton控件关联到单个组中。
观察
在UI中,只要在页面上放置至少2个或更多具有相同group属性值的RadioButton控件,两者在视觉上产生相同的结果。
UI示例代码如下:
asp: RadioButton
<asp:RadioButton ID="b2b" text="B to B" checked="true" runat ="server" GroupName="businesstype" />
<asp:RadioButton ID="b2c" text="B to C" runat ="server" GroupName="businesstype" />

asp:RadioButtonList

<asp:RadioButtonList ID="businesstype" runat="server" >
    <asp:ListItem Selected="True" Value="0">B to B</asp:ListItem>
    <asp:ListItem Value="1">B to C</asp:ListItem>
</asp:RadioButtonList>

使用每种的指导方针是什么?

1
除了问题的措辞可能有些问题之外,你是否知道这个回答被踩的原因?对我来说,这个回答很有用,并且从一个关于这个问题标题的精确搜索中出现。如果是因为问题措辞/用语而被踩,那么为什么不编辑问题而是选择踩呢?今天下班后我会抽时间编辑这个问题。 - qxotk
3个回答

3

1. 单选按钮列表(RadioButtonList)

单选按钮列表是一个带有一组单选按钮的控件。 它派生自ListControl类,因此它的工作方式类似于其他列表控件,如ListBox、DropDownList和CheckBoxList。 要为按钮提供标题,可以使用Text属性。您无法在两个按钮之间插入文本。 使用“SelectedIndexChanged”事件,您将获得所选按钮的值(“RadioButtonList1.SelectedValue”)。

例如:

private void Bind()
{
  RadioButtonList1.DataSource = dsEmployees;
  RadioButtonList1.DataTextField = "EmployeeName";
  RadioButtonList1.DataValueField = "EmployeeID";
  RadioButtonList1.DataBind();
} 

如果您正在使用HTML,您可以使用以下标签来创建网页内容:
<asp:RadioButtonList ID="RadioButtonList1" runat="server" 
  RepeatDirection="Horizontal" 
  onselectedindexchanged="RadioButtonList1_SelectedIndexChanged">
  <asp:ListItem Text="Male" Value="1" ></asp:ListItem>
  <asp:ListItem Text="Female" Value="2" ></asp:ListItem>
</asp:RadioButtonList>

2. 单选按钮

“RadioButton”是一个单一的控件,它由“CheckBox”类派生而来。您需要设置GroupName属性来标识一个组。此外,“CheckedChanged”事件的事件处理程序将帮助我们完成一些工作。还有一件事是您需要为每个单选按钮编写单独的处理程序。

例如:

<asp:RadioButton ID="RadioButton1" runat="server" GroupName="Gender" 
   AutoPostBack="true" oncheckedchanged="RadioButton1_CheckedChanged" Text="Male" />
   <asp:RadioButton ID="RadioButton2" runat="server" GroupName="Gender" 
 AutoPostBack="true" oncheckedchanged="RadioButton2_CheckedChanged" Text="Female" />

0

您可以通过访问 RadioButtonList 的 ListItem 集合来获取所选索引。

您可以点击此处了解更多细节

相比之下,RadioButtonList 控件是一个单一的控件,作为一组单选按钮列表项的父控件。

它派生自基类 ListControl Class,因此与 ListBoxDropDownListCheckBoxList Web 服务器控件的工作方式非常相似。因此,与其他列表 Web 服务器控件 相同,许多处理 RadioButtonList 控件的过程也是相同的。


0
一个 asp:radiobuttonlist 创建一组单选按钮,确保当一个被选中时,其他的都会被取消选择,而 asp:radiobutton 不在组内,因此不能通过点击其他单选按钮来取消选择。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接