RadioButtonList 类

表示封装一组单选按钮控件的列表控件。

**命名空间:**System.Web.UI.WebControls
**程序集:**System.Web(在 system.web.dll 中)

语法

声明
<ValidationPropertyAttribute("SelectedItem")> _
Public Class RadioButtonList
    Inherits ListControl
    Implements IRepeatInfoUser, INamingContainer, IPostBackDataHandler
用法
Dim instance As RadioButtonList
[ValidationPropertyAttribute("SelectedItem")] 
public class RadioButtonList : ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler
[ValidationPropertyAttribute(L"SelectedItem")] 
public ref class RadioButtonList : public ListControl, IRepeatInfoUser, INamingContainer, IPostBackDataHandler
/** @attribute ValidationPropertyAttribute("SelectedItem") */ 
public class RadioButtonList extends ListControl implements IRepeatInfoUser, INamingContainer, 
    IPostBackDataHandler
ValidationPropertyAttribute("SelectedItem") 
public class RadioButtonList extends ListControl implements IRepeatInfoUser, INamingContainer, 
    IPostBackDataHandler

备注

RadioButtonList 控件为网页开发人员提供了一组单选按钮,这些按钮可以通过数据绑定动态生成。该控件包含一个 Items 集合,集合中的成员与列表中的各项相对应。若要确定选择了哪一项,请测试列表的 SelectedItem 属性。

可以用 RepeatLayoutRepeatDirection 属性指定如何呈现列表。如果将 RepeatLayout 设置为 RepeatLayout.Table(默认设置),列表将呈现在表中。如果设置为 RepeatLayout.Flow,列表将不以表格形式呈现。默认情况下,RepeatDirection 设置为 RepeatDirection.Vertical。将该属性设置为 RepeatDirection.Horizontal 时,列表将水平呈现。

警告

此控件可用来显示用户输入,而该输入可能包含恶意的客户端脚本。在应用程序中显示从客户端发送来的任何信息之前,请检查它们是否包含可执行脚本、SQL 语句或其他代码。在 RadioButtonList 控件中显示输入文本之前,可以使用验证控件对用户输入进行验证。ASP.NET 提供输入请求验证功能以阻止用户输入中的脚本和 HTML。有关更多信息,请参见 保证标准控件的安全如何:通过对字符串应用 HTML 编码在 Web 应用程序中防止脚本侵入在 ASP.NET 网页中验证用户输入

辅助功能

默认情况下为此控件呈现的标记可能不符合 Web 内容辅助功能准则 1.0 (WCAG) 中优先级为 1 的准则等辅助功能标准。有关此控件的辅助功能支持的详细信息,请参见 ASP.NET 控件和辅助功能

主题 位置
演练:到自定义业务对象的数据绑定 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:设置列表 Web 服务器控件中的选定内容 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 RadioButtonList Web 服务器控件 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建和使用 ASP.NET 母版页 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:根据数据源来填充列表 Web 服务器控件 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 RadioButton Web 服务器控件 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:在列表 Web 服务器控件中添加项 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:在 Visual Web Developer 中创建和使用 ASP.NET 母版页 使用 Visual Web Developer 生成应用程序
如何:设置列表 Web 服务器控件中的选定内容 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应列表 Web 服务器控件中的更改 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:设置 RadioButtonList Web 服务器控件中的布局 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:设置和获取 RadioButton Web 服务器控件中的选择 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:以编程方式向 ASP.NET 网页添加控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:根据数据源来填充列表 Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 RadioButtonList Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:到自定义业务对象的数据绑定 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在列表 Web 服务器控件中添加项 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:确定 List Web 服务器控件中的所选内容 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 RadioButton Web 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:设置列表 Web 服务器控件中的选定内容 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应列表 Web 服务器控件中的更改 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:设置 RadioButtonList Web 服务器控件中的布局 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:设置和获取 RadioButton Web 服务器控件中的选择 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:以编程方式向 ASP.NET 网页添加控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:根据数据源来填充列表 Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 RadioButtonList Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:到自定义业务对象的数据绑定 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:在列表 Web 服务器控件中添加项 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:确定 List Web 服务器控件中的所选内容 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 RadioButton Web 服务器控件 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:响应列表 Web 服务器控件中的更改 生成 ASP .NET Web 应用程序
如何:设置 RadioButtonList Web 服务器控件中的布局 生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 RadioButtonList Web 服务器控件 生成 ASP .NET Web 应用程序
如何:设置和获取 RadioButton Web 服务器控件中的选择 生成 ASP .NET Web 应用程序
如何:以编程方式向 ASP.NET 网页添加控件 生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加各个 RadioButton Web 服务器控件 生成 ASP .NET Web 应用程序
如何:确定 List Web 服务器控件中的所选内容 生成 ASP .NET Web 应用程序
如何:从数据源填充 List Web 服务器控件 生成 ASP .NET Web 应用程序
如何:在 List Web 服务器控件中添加项 生成 ASP .NET Web 应用程序
如何:设置 List Web 服务器控件中的所选内容 生成 ASP .NET Web 应用程序

示例

下面的代码示例演示如何以编程方式修改 RadioButtonList 控件的显示。

提示

下面的代码示例使用单文件代码模型,将该模型直接复制到代码隐藏文件后可能无法正常工作。此代码示例必须被复制到具有 .aspx 扩展名的空文本文件中。有关 Web 窗体代码模型的更多信息,请参见 ASP.NET 网页代码模型

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
 <head>
     <script language="VB" runat="server">

    Sub Button1_Click(Source As Object, e As EventArgs)
        If RadioButtonList1.SelectedIndex > - 1 Then
            Label1.Text = "You selected: " & RadioButtonList1.SelectedItem.Text
        End If
    End Sub

    Sub chkLayout_CheckedChanged(sender As Object, e As EventArgs)        
        If chkLayout.Checked = True Then
            RadioButtonList1.RepeatLayout = RepeatLayout.Table
        Else
            RadioButtonList1.RepeatLayout = RepeatLayout.Flow
        End If
    End Sub

    Sub chkDirection_CheckedChanged(sender As Object, e As EventArgs)        
        If chkDirection.Checked = True Then
            RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal
        Else
            RadioButtonList1.RepeatDirection = RepeatDirection.Vertical
        End If
    End Sub
 
     </script>
 
 </head>
 <body>
 
     <h3>RadioButtonList Example</h3>
 
     <form runat=server>
 
         <asp:RadioButtonList id=RadioButtonList1 runat="server">
            <asp:ListItem>Item 1</asp:ListItem>
            <asp:ListItem>Item 2</asp:ListItem>
            <asp:ListItem>Item 3</asp:ListItem>
            <asp:ListItem>Item 4</asp:ListItem>
            <asp:ListItem>Item 5</asp:ListItem>
            <asp:ListItem>Item 6</asp:ListItem>
         </asp:RadioButtonList>
 
         <p>
         
         <asp:CheckBox id=chkLayout OnCheckedChanged="chkLayout_CheckedChanged" Text="Display Table Layout" Checked=true AutoPostBack="true" runat="server" />
 
         <br>
         
         <asp:CheckBox id=chkDirection OnCheckedChanged="chkDirection_CheckedChanged" Text="Display Horizontally" AutoPostBack="true" runat="server" />
 
         <p>
         
         <asp:Button id=Button1 Text="Submit" onclick="Button1_Click" runat="server"/>
 
         <p>
         
         <asp:Label id=Label1 font-name="Verdana" font-size="8pt" runat="server"/>
 
     </form>
 
 </body>
 </html>
   
<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
 <head>
     <script language="C#" runat="server">
 
        void Button1_Click(object Source, EventArgs e) 
        {
           if (RadioButtonList1.SelectedIndex > -1) 
           {  
              Label1.Text = "You selected: " + RadioButtonList1.SelectedItem.Text;
           }
        }
 
        void chkLayout_CheckedChanged(Object sender, EventArgs e) 
        {
        
           if (chkLayout.Checked == true) 
           {
              RadioButtonList1.RepeatLayout = RepeatLayout.Table;
           }
           else 
           {
              RadioButtonList1.RepeatLayout = RepeatLayout.Flow;
           }     
        }
        
        void chkDirection_CheckedChanged(Object sender, EventArgs e) 
        {
        
           if (chkDirection.Checked == true) 
           {
              RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal;
           }
           else 
           {
              RadioButtonList1.RepeatDirection = RepeatDirection.Vertical;
           }  
        }
 
     </script>
 
 </head>
 <body>
 
     <h3>RadioButtonList Example</h3>
 
     <form runat=server>
 
         <asp:RadioButtonList id=RadioButtonList1 runat="server">
            <asp:ListItem>Item 1</asp:ListItem>
            <asp:ListItem>Item 2</asp:ListItem>
            <asp:ListItem>Item 3</asp:ListItem>
            <asp:ListItem>Item 4</asp:ListItem>
            <asp:ListItem>Item 5</asp:ListItem>
            <asp:ListItem>Item 6</asp:ListItem>
         </asp:RadioButtonList>
 
         <p>
         
         <asp:CheckBox id=chkLayout OnCheckedChanged="chkLayout_CheckedChanged" Text="Display Table Layout" Checked=true AutoPostBack="true" runat="server" />
 
         <br>
         
         <asp:CheckBox id=chkDirection OnCheckedChanged="chkDirection_CheckedChanged" Text="Display Horizontally" AutoPostBack="true" runat="server" />
 
         <p>
         
         <asp:Button id=Button1 Text="Submit" onclick="Button1_Click" runat="server"/>
 
         <p>
         
         <asp:Label id=Label1 font-name="Verdana" font-size="8pt" runat="server"/>
 
     </form>
 
 </body>
 </html>
   
<%@ Page Language="JScript" AutoEventWireup="True" %>
<html>
 <head>
     <script language="JScript" runat="server">
 
        function Button1_Click(Source : System.Object, e : EventArgs) 
        {
           if (RadioButtonList1.SelectedIndex > -1) 
           {  
              Label1.Text = "You selected: " + RadioButtonList1.SelectedItem.Text;
           }
        }
 
        function chkLayout_CheckedChanged(sender : System.Object, e : EventArgs) 
        {
        
           if (chkLayout.Checked == true) 
           {
              RadioButtonList1.RepeatLayout = RepeatLayout.Table;
           }
           else 
           {
              RadioButtonList1.RepeatLayout = RepeatLayout.Flow;
           }     
        }
        
        function chkDirection_CheckedChanged(sender : System.Object, e : EventArgs) 
        {
        
           if (chkDirection.Checked == true) 
           {
              RadioButtonList1.RepeatDirection = RepeatDirection.Horizontal;
           }
           else 
           {
              RadioButtonList1.RepeatDirection = RepeatDirection.Vertical;
           }  
        }
 
     </script>
 
 </head>
 <body>
 
     <h3>RadioButtonList Example</h3>
 
     <form runat=server>
 
         <asp:RadioButtonList id=RadioButtonList1 runat="server">
            <asp:ListItem>Item 1</asp:ListItem>
            <asp:ListItem>Item 2</asp:ListItem>
            <asp:ListItem>Item 3</asp:ListItem>
            <asp:ListItem>Item 4</asp:ListItem>
            <asp:ListItem>Item 5</asp:ListItem>
            <asp:ListItem>Item 6</asp:ListItem>
         </asp:RadioButtonList>
 
         <p>
         
         <asp:CheckBox id=chkLayout OnCheckedChanged="chkLayout_CheckedChanged" Text="Display Table Layout" Checked=true AutoPostBack="true" runat="server" />
 
         <br>
         
         <asp:CheckBox id=chkDirection OnCheckedChanged="chkDirection_CheckedChanged" Text="Display Horizontally" AutoPostBack="true" runat="server" />
 
         <p>
         
         <asp:Button id=Button1 Text="Submit" onclick="Button1_Click" runat="server"/>
 
         <p>
         
         <asp:Label id=Label1 font-name="Verdana" font-size="8pt" runat="server"/>
 
     </form>
 
 </body>
 </html>
   

.NET Framework 安全性

继承层次结构

System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
       System.Web.UI.WebControls.BaseDataBoundControl
         System.Web.UI.WebControls.DataBoundControl
           System.Web.UI.WebControls.ListControl
            System.Web.UI.WebControls.RadioButtonList

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

RadioButtonList 成员
System.Web.UI.WebControls 命名空间
ListControl 类
RepeatDirection
RepeatLayout

其他资源

RadioButton 和 RadioButtonList Web 服务器控件
保证标准控件的安全