我需要知道RadioButton和RadioButtonList之间的区别,以及在决定使用哪一个时使用的准则是什么?
我对此进行了研究并决定在此发布我的发现,以帮助说明我发现的差异,这些差异应该有助于澄清我的问题:
我学到了什么:
单选按钮
用于一次显示一个RadioButton。可能需要设置组属性以将多个RadioButton控件关联到一个组中。
单选按钮列表
用于显示一组RadioButton控件,自动提供将所有包含的RadioButton控件关联到一个组中的组属性。
观察
视觉上,两者在UI中产生相同的结果,前提是一个在页面上放置至少2个或更多的RadioButton控件,并且组属性具有相同的值。
UI示例代码如下
ASP:单选按钮
<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>
每种方法的使用指南是什么?
答案 0 :(得分:2)
<强> 1。单选按钮列表强>
RadioButtonList是一个带有RadioButtons列表的控件。 这是从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" />
答案 1 :(得分:0)
您可以在RadioButtonList
中获取所选索引,因为它可用于收集ListItem。
相比之下,RadioButtonList
控件是一个控件,充当单选按钮列表项集合的父控件。
它派生自基础ListControl Class
,因此与ListBox
,DropDownList
和CheckBoxList
Web服务器控件非常相似。因此,使用RadioButtonList
控件的许多过程与其他列表Web server controls
的过程相同。
答案 2 :(得分:0)
asp:radiobuttonlist
创建一组单选按钮,确保在选择其中一个时,取消选择其他单选按钮,而asp:radiobutton
不在组内,因此无法通过单击其他单选按钮取消选择。