C# RadioButon和RadioButton列表有什么区别?

C# RadioButon和RadioButton列表有什么区别?,c#,asp.net,vb.net,C#,Asp.net,Vb.net,我需要知道RadioButton和RadioButtonList之间的区别,以及在决定使用哪一个时使用的准则是什么 我对此进行了研究,并决定将我的发现发布在这里,以帮助说明我发现的差异,这些差异有助于澄清我的问题: 我学到的是: 单选按钮 <asp:RadioButton ID="b2b" text="B to B" checked="true" runat ="server" GroupName="businesstype" /> <asp:RadioButton ID="b

我需要知道RadioButton和RadioButtonList之间的区别,以及在决定使用哪一个时使用的准则是什么

我对此进行了研究,并决定将我的发现发布在这里,以帮助说明我发现的差异,这些差异有助于澄清我的问题:

我学到的是:

单选按钮

<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" />
用于一次显示一个单选按钮。可能需要设置组属性以将多个RadioButton控件关联到一个组中

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>
用于显示一组RadioButton控件,自动提供将所有包含的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" />
从视觉上看,两者在UI中产生相同的结果,前提是在页面上放置至少2个或多个RadioButton控件,并使用相同的group属性值

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>

B对B
B到C

每种方法的使用指南是什么?

在收集列表项时,您可以在
RadioButtonList
中获得所选索引

你可以

相反,
RadioButtonList
控件是一个单独的控件,充当单选按钮列表项集合的父控件


它源于一个基本的
ListControl类
,因此与
ListBox
DropDownList
CheckBoxList
Web服务器控件非常相似。因此,使用
RadioButtonList
控件的许多步骤与使用其他列表
Web服务器控件的步骤相同。Radio按钮列表

<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>
RadioButtonList是带有单选按钮列表的单个控件。 这是从ListControl类派生的。因此,这将类似于其他列表控件,如ListBox、DropDownList和CheckBoxList。 要为按钮提供标题,可以使用Text属性。不能在两个按钮之间插入文本。 使用“SelectedIndexChanged”事件,您将获得SelectedButtons值(“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。单选按钮

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


一个
asp:radiobuttonlist
创建一组单选按钮,确保在选择一个按钮时,其他按钮被取消选择,而
asp:radiobutton
不在一个组内,因此无法通过单击其他单选按钮取消选择。

想知道投票失败的原因吗(除了问题中的措辞之外)?。这对我很有用,从对该问题确切标题的良好搜索中可以看出。如果对问题措辞/措辞投反对票,那么为什么不编辑问题而不是投反对票?今天下班后我将编辑该问题。