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

ASP.NET
asp.net 动态生成表格
asp.net 程序优化精选
DataGridView自动调整行高和行宽
asp.net+js实现的ajax sugguest搜索提示效果
asp.net 将设有过期策略的项添加到缓存中
asp.net SqlDataAdapter对象使用札记
DataGrid 动态添加模板列 实现代码
asp.net 设置GridView的选中行
the sourcesafe database has been locked by the administrator之解决方法
asp.net 退出登陆(解决退出后点击浏览器后退问题仍然可回到页面问题)
Asp.Net HttpHandler 妙用
ASP.NET 保留文件夹详解
asp.net 中将表单提交到另一页 Code-Behind(代码和html在不同的页面)
SqlDataSource 链接Access 数据
asp.net GridView的删除对话框的两种方法
asp.net 按字节检查包含全半角的文字
asp.net String.IsNullOrEmpty 方法
asp.net System.Net.Mail 发送邮件
c# 读取文件内容存放到int数组 array.txt
asp.net Split分割字符串的方法

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 111 ::
收藏到网摘: 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>