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

ASP.NET
ASP.NET在上传文件时对文件类型的高级判断的代码
JQuery运用ajax注册用户实例(后台asp.net)
Asp.net与SQLserver一起打包部署安装图文教程
asp.net 上传下载输出二进制流实现代码
asp.net(C#)解析Json的类代码
asp.net 截取字符串代码
asp.net ubb使用代码
asp.net XML文件操作实现代码
asp.net利用HttpModule实现防sql注入
ASP.NET(C#)中操作SQLite数据库实例
asp.net(c#)ref,out ,params的区别
asp.net(C#)防sql注入组件的实现代码
asp.net FCKeditor自定义非空验证
Asp.net TreeView来构建用户选择输入的方法 推荐
asp.net(C#)函数对象参数传递的问题
Asp.net中的GridView导出遇到的两个问题和解决方法
asp.Net 中获取一周第一天,一月第一天等实现代码
asp.net MaxLengthValidator 最大长度验证控件代码
C# 通用文件上传类
asp.net 自定义控件实现无刷新上传图片,立即显示缩略图,保存图片缩略图

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


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