vb.net RadioButton 和 RadioButtonList 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27310452/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:33:02  来源:igfitidea点击:

What is the difference between the RadioButon and RadioButtonList?

c#asp.netvb.net

提问by Lawrance

I need to know the difference between a RadioButton and a RadioButtonList, and what are the guidelines to use when deciding which one to use?

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

I researched this and decided to post my findings here to help illustrate the differences I found that should help clarify my question:

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

What I Learned:

我学到的是:

RadioButton

单选按钮

Used to display a single RadioButton at a time. Likely requires setting group attribute to associate multiple RadioButton controls into a group.

用于一次显示一个 RadioButton。可能需要设置组属性以将多个 RadioButton 控件关联到一个组中。

RadioButtonList

单选按钮列表

Used to Display a group of RadioButton controls, automatically providing the group attribute associating all the included RadioButton controls into a single group.

用于显示一组 RadioButton 控件,自动提供 group 属性,将所有包含的 RadioButton 控件关联到一个组中。

Observation

观察

Visually, both produce the same result in the UI, provided one places at least 2 or more RadioButton controls on the page with the same value for the group attribute.

从视觉上看,只要在页面上放置至少 2 个或更多 RadioButton 控件且具有相同的 group 属性值,两者都会在 UI 中产生相同的结果。

UI Sample Code follows

UI示例代码如下

asp:RadioButton

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:单选按钮列表

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

What are the guidelines for the use of each?

每个的使用指南是什么?

回答by Shiji kumaran

1. RadioButtonList

1. 单选按钮列表

RadioButtonList is a single control with a list of RadioButtons. This is derived from ListControl class. So this will work similar to other list controls like ListBox, DropDownList and CheckBoxList. For giving a caption for buttons you can use the Text property. You cannot insert a text in between two buttons. Using the “SelectedIndexChanged” event you will get the selected buttons value (“RadioButtonList1.SelectedValue”).

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

for e.g

例如

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

If you are using HTML

如果您使用 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

2. 单选按钮

RadioButton” is a single control, it is derived from “CheckBox” Class. You have to set the GroupName property to identify a group. Also the event handler for the event “CheckedChanged” will help us to do some job. Another one thing is you have to write separate handlers for each radio button.

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

For e.g.:

例如:

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

回答by ????

You can get the selected Index in RadioButtonListas it work on collection of ListItem.

您可以在RadioButtonList处理 ListItem 集合时获取所选索引。

You can visit here for more detail

您可以访问这里了解更多详情

In contrast, the RadioButtonListcontrol is a single control that acts as a parent control for a collection of radio button list items.

相比之下,该RadioButtonList控件是单个控件,充当单选按钮列表项集合的父控件。

It derives from a base ListControl Class, and therefore works much like the ListBox, DropDownList, and CheckBoxListWeb server controls. Therefore, many of the procedures for working with the RadioButtonListcontrol are the same as they are for the other list Web server controls.

它从基础派生ListControl Class的,因此作品很像ListBoxDropDownListCheckBoxListWeb服务器控件。因此,使用RadioButtonList控件的许多程序与其他列表的程序相同Web server controls

回答by Varun Babu Pozhath

An asp:radiobuttonlistcreates a group of radiobuttons that ensures when one is selected, the others are deselected whereas asp:radiobuttonis not within a group and therefore cannot be deselected by clicking other radio buttons.

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