当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > Web服务器控件:RadioButtonList控件

ASP.NET
ewebeditor在.net的使用方法
Server.Transfer,Response.Redirect的区别
ASP.NET2.0+SQL Server2005构建多层应用
ASP.NET 2.0 中收集的小功能点(转)
ASP.Net全局变量的设置和读取方法
数据库开发总结(ADO.NET小结)
ASP.net(c#)打造24小时天气预报及实时天气
发布WEB站点时出现Server Application Unavailable
在asp.net中实现datagrid checkbox 全选的方法
ASP.NET 2.0 URL映射技巧
ConfiguraionSource节点及多个配置文件的应用
SqlConnection.ConnectionString相关关键字
如何在WebForm中使用javascript防止连打(双击)
看到本质而不是现象--解决ASP.NET CS0016的问题
学会区分Visual Studio 2005,Visual Studio 2005 Team System和MSDN Premium 订阅的各个版本
ASP.NET 入门的五个步骤
ASP.NET 高性能分页代码
动态ItemTemplate的实现(译) - item,template
遍历Hashtable 的几种方法
通过VS中的数据源选择对话框简单实现数据库连接配置

ASP.NET 中的 Web服务器控件:RadioButtonList控件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 118 ::
收藏到网摘: n/a

阅读此文请先查看软晨学习网的:ASP.NET入门教程:Web服务器控件,简单讲述了Web服务器控件的使用方法。

定义和用法

RadioButtonList 控件用于创建单选按钮组。创建一组单选按钮。此控件支持绑定到数据源。

RadioButtonList 控件中的每个可选项是通过 ListItem 元素来定义的!

提示:该控件支持数据绑定!

属性

属性 描述 .NET
CellPadding 单元格边框与内容之间的像素数。 1.0
CellSpacing 表格单元格之间的像素数。 1.0
RepeatColumns 当显示单选按钮组时要使用的列数。 1.0
RepeatDirection 规定单选按钮组应水平重复还是垂直重复。 1.0
RepeatLayout 单选按钮组的布局。 1.0
runat 规定该控件是服务器控件。必须设置为 "server"。 1.0
TextAlign 文本应出现在单选按钮的哪一侧(左侧还是右侧)。 1.0

ListControl 标准属性

AppendDataBoundItems, AutoPostBack, CausesValidation, DataTextField,
DataTextFormatString, DataValueField, Items, runat, SelectedIndex, SelectedItem,
SelectedValue, TagKey, Text, ValidationGroup, OnSelectedIndexChanged

ListControl 控件包括列表控件的所有基本功能。继承自此控件的控件包括:CheckBoxList, DropDownList, ListBox 以及 RadioButtonList 控件。

Web 控件标准属性

AccessKey, Attributes, BackColor, BorderColor, BorderStyle, BorderWidth,
CssClass, Enabled, Font, EnableTheming, ForeColor, Height, IsEnabled,
SkinID, Style, TabIndex, ToolTip, Width

控件标准属性

AppRelativeTemplateSourceDirectory, BindingContainer, ClientID, Controls,
EnableTheming, EnableViewState, ID, NamingContainer, Page, Parent, Site,
TemplateControl, TemplateSourceDirectory, UniqueID, Visible

语法

<asp:RadioButtonList
    AccessKey="string"
    AppendDataBoundItems="True|False"
    AutoPostBack="True|False"
    BackColor="color name|#dddddd"
    BorderColor="color name|#dddddd"
    BorderStyle="NotSet|None|Dotted|Dashed|Solid|Double|Groove|Ridge|
        Inset|Outset"
    BorderWidth="size"
    CausesValidation="True|False"
    CellPadding="integer"
    CellSpacing="integer"
    CssClass="string"
    DataMember="string"
    DataSource="string"
    DataSourceID="string"
    DataTextField="string"
    DataTextFormatString="string"
    DataValueField="string"
    Enabled="True|False"
    EnableTheming="True|False"
    EnableViewState="True|False"
    Font-Bold="True|False"
    Font-Italic="True|False"
    Font-Names="string"
    Font-Overline="True|False"
    Font-Size="string|Smaller|Larger|XX-Small|X-Small|Small|Medium|
        Large|X-Large|XX-Large"
    Font-Strikeout="True|False"
    Font-Underline="True|False"
    ForeColor="color name|#dddddd"
    Height="size"
    ID="string"
    OnDataBinding="DataBinding event handler"
    OnDataBound="DataBound event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnSelectedIndexChanged="SelectedIndexChanged event handler"
    OnTextChanged="TextChanged event handler"
    OnUnload="Unload event handler"
    RepeatColumns="integer"
    RepeatDirection="Horizontal|Vertical"
    RepeatLayout="Table|Flow"
    runat="server"
    SelectedIndex="integer"
    SelectedValue="string"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    TextAlign="Left|Right"
    ToolTip="string"
    ValidationGroup="string"
    Visible="True|False"
    Width="size"
>
            <asp:ListItem
                Enabled="True|False"
                Selected="True|False"
                Text="string"
                Value="string"
            />
</asp:RadioButtonList>

备注:RadioButtonList 控件使您能够创建单项选择的单选按钮组,可以通过绑定到数据源动态生成这个组。若要指定要在 RadioButtonList 控件中显示的项,请针对每项在 RadioButtonList 控件的开始标记和结束标记之间放置一个 ListItem 元素。

注意:还可以使用 RadioButton 控件。对于使用数据绑定创建一组单选按钮,RadioButtonList 控件更方便,而单个 RadioButton 控件则可以更好地控制布局。

RadioButtonList 控件还支持数据绑定。若要将控件绑定到数据源,请首先创建数据源(如 ArrayList 对象),该数据源包含要显示在控件中的项。下一步,使用 DataBind 方法将数据源绑定到 RadioButtonList 控件。使用 DataTextField 和 DataValueField 属性分别指定数据源中哪个字段绑定到控件中每个列表项的 Text 和 Value 属性。现在,RadioButtonList 控件将显示数据源中的信息。加此信息软晨学习网(ruanchen.com)发布目的是为了防止你变懒!ruanchen.com不主张采集!

若要确定 RadioButtonList 控件中的选定项,请循环访问 Items 集合并测试该集合中每一项的 Selected 属性。

可以使用 RepeatLayout 和 RepeatDirection 属性指定如何呈现列表。如果将 RepeatLayout 设置为 Table(默认设置),将以表格形式呈现列表。如果将它设置为 Flow,则不会使用任何表格结构呈现列表。默认情况下,RepeatDirection 设置为 Vertical。将此属性设置为 Horizontal 可以水平呈现该列表。

警告:文本在 RadioButtonList 控件中显示之前并非 HTML 编码形式。这使得可以在文本中的 HTML 标记中嵌入脚本。如果控件的值是由用户输入的,请务必要对输入值进行验证以防止出现安全漏。

实例

下面的示例演示如何使用 RadioButtonList 控件向用户显示一组互相排斥的选项。

Visual Basic

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

C#

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

JScript

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