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

ASP.NET
谈谈ASP.net2.0中App_GlobalResources的用途
.Net Framework 4.0 功能介绍
ASP.NET教程:浅谈Asp.net实现的邮件发送引擎
ASP.NET教程:绝对路径与相对路径的拼合方法
ASP.NET开发电子商务网站学习经验
ASP.NET与PHP构建web程序的方法的优缺点
ASp.NET教程:页面传值的五种方法
ASP.NET教程:数据缓存和输出缓存
ASP.NET比拼PHP的测试环境
ASP.NET MVC 2的客户端验证扩展
ASP.NET实例:无刷新的文件上传
ASP.NET实例: GridView删除时弹出确认对话框
ASP.NET获取不到js写的cookie解决方法
ASP.NET MVC教程:数据库表的增删改
ASP.NET教程:form验证用户登录的Cookie
如何让.NET程序脱离.NET框架
ASP.NET开发网站程序常见错误汇总
C#教程:匿名类型和隐式类型变量的区别
C#3.0教程:自动属性和扩展方法
使用XmlDocument读取XML节点所有数据

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


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

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

定义和用法

RadioButton 控件用于显示单选按钮。在页上创建一个单选按钮。可将多个单选按钮分为一组以提供一组互相排斥的选项。

提示:如需创建一系列使用数据绑定的单选按钮,请使用 RadioButtonList 控件!

属性

属性 描述
AutoPostBack 布尔值,规定在 Checked 属性被改变后,是否立即回传表单。默认是 false。
Checked 布尔值,规定是否选定单选按钮。
id 控件的唯一 id。
GroupName 该单选按钮所属控件组的名称。
OnCheckedChanged 当 Checked 被改变时,被执行的函数的名称。
runat 规定该控件是服务器控件。必须设置为 "server"。
Text 单选按钮旁边的文本。
TextAlign 文本应出现在单选按钮的哪一侧(左侧还是右侧)。

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:RadioButton
    AccessKey="string"
    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"
    Checked="True|False"
    CssClass="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"
    GroupName="string"
    Height="size"
    ID="string"
    OnCheckedChanged="CheckedChanged event handler"
    OnDataBinding="DataBinding event handler"
    OnDisposed="Disposed event handler"
    OnInit="Init event handler"
    OnLoad="Load event handler"
    OnPreRender="PreRender event handler"
    OnUnload="Unload event handler"
    runat="server"
    SkinID="string"
    Style="string"
    TabIndex="integer"
    Text="string"
    TextAlign="Left|Right"
    ToolTip="string"
    ValidationGroup="string"
    Visible="True|False"
    Width="size"
/>

备注:RadioButton 服务器控件在 Web 窗体页上创建一个单选按钮。通过设置 Text 属性指定要在控件中显示的文本。该文本可显示在单选按钮的左侧或右侧。设置 TextAlign 属性以控制该文本显示在哪一侧。如果为每个 RadioButton 控件指定了相同的 GroupName,则可以将多个单选按钮分为一组。将单选按钮分为一组将只允许从该组中进行互相排斥的选择。

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

若要确定 RadioButton 控件是否已选中,请测试 Checked 属性。

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

实例:

下面的示例演示如何使用 RadioButton 控件为用户提供一组互斥的选项。

Visual Basic

<%@ Page Language="VB" AutoEventWireup="True" %>
<html>
 <head>
     <script language="VB" runat="server">
    Sub SubmitBtn_Click(Sender As Object, e As EventArgs)       
        If Radio1.Checked Then
            Label1.Text = "You selected " & Radio1.Text
        ElseIf Radio2.Checked Then
            Label1.Text = "You selected " & Radio2.Text
        ElseIf Radio3.Checked Then
            Label1.Text = "You selected " & Radio3.Text
        End If
    End Sub
     </script>
 </head>
 <body>
     <h3>RadioButton Example</h3>
     <form runat=server>    
         <h4>Select the type of installation you want to perform:</h4>    
         <asp:RadioButton id=Radio1 Text="Typical" Checked="True" GroupName="RadioGroup1" runat="server" /><br>        
         This option installs the features most typically used.  <i>Requires 1.2 MB disk space.</i><p>            
         <asp:RadioButton id=Radio2 Text="Compact" GroupName="RadioGroup1" runat="server"/><br>        
         This option installs the minimum files required to run the product.  <i>Requires 350 KB disk space.</i><p>         
         <asp:RadioButton id=Radio3 runat="server" Text="Full" GroupName="RadioGroup1" /><br>        
         This option installs all features for the product.  <i>Requires 4.3 MB disk space.</i><p>
         <asp:button text="Submit" OnClick="SubmitBtn_Click" runat=server/>
         <asp:Label id=Label1 font-bold="true" runat="server" />            
     </form>
 </body>
 </html>

C#

<%@ Page Language="C#" AutoEventWireup="True" %>
<html>
 <head>
     <script language="C#" runat="server">
         void SubmitBtn_Click(Object Sender, EventArgs e) {        
             if (Radio1.Checked) {
                 Label1.Text = "You selected " + Radio1.Text;
             }
             else if (Radio2.Checked) {
                 Label1.Text = "You selected " + Radio2.Text;
             }
             else if (Radio3.Checked) {
                 Label1.Text = "You selected " + Radio3.Text;
             }
         }
     </script>
 </head>
 <body>
     <h3>RadioButton Example</h3>
     <form runat=server> 
         <h4>Select the type of installation you want to perform:</h4> 
         <asp:RadioButton id=Radio1 Text="Typical" Checked="True" GroupName="RadioGroup1" runat="server" /><br>
         This option installs the features most typically used.  <i>Requires 1.2 MB disk space.</i><p>            
         <asp:RadioButton id=Radio2 Text="Compact" GroupName="RadioGroup1" runat="server"/><br>        
         This option installs the minimum files required to run the product.  <i>Requires 350 KB disk space.</i><p>
         <asp:RadioButton id=Radio3 runat="server" Text="Full" GroupName="RadioGroup1" /><br>        
         This option installs all features for the product.  <i>Requires 4.3 MB disk space.</i><p>
         <asp:button text="Submit" OnClick="SubmitBtn_Click" runat=server/>
         <asp:Label id=Label1 font-bold="true" runat="server" /> 
     </form>
 </body>
 </html>