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

ASP.NET
asp.net Execl的添加,更新操作实现代码
asp.net 生成曲线图实现代码
从外部的js文件中获取ASPX页面的控件ClientID
asp.net 因为数据库正在使用的解决方法
asp.net Repeater 自递增
asp.net 实现防迅雷等下载工具盗链
ASP.NET封装的SQL数据库访问类
asp.net 通过指定IP地址得到当前的网络上的主机的域名
aspx 服务器架设问题解决
ASP.NET Session使用详解
Asp.net 5种页面转向方法
ASP.NET 用户多次登录的解决方法
ASP.NET下母版页和内容页中的事件发生顺序整理
asp.net 程序性能优化的七个方面 (c#(或vb.net)程序改进)
从客户端检测到有潜在危险的Request.Form值的asp.net代码
asp.net CommunityServer中的wwwStatus
在应用程序级别之外使用注册为allowDefinition=''MachineToApplication''的节是错误的
.net开发人员常犯的错误分析小结
ASP.net Substitution 页面缓存而部分不缓存的实现方法
asp.net 生成静态时的过滤viewstate的实现方法

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


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