RadioButton控件

1. RadioButton控件的概述

RadioButton控件是一种单选按钮控件,用户可以在页面中添加一组RadioButton控件,通过为所有的单选按钮分配相同的GroupName(组名),来强制执行从给出的所有选项集中仅选择一个选项。图1RadioButton控件。

1  RadioButton控件

1RadioButton控件常用属性

RadioButton控件常用属性及说明如表1所示。

1                            RadioButton控件常用的属性

属性名称

属性值

AutoPostBack

获取或设置一个值,该值指示在单击RadioButton控件时,是否自动回发到服务器

CausesValidation

获取或设置一个值,该值指示在单击RadioButton控件时,是否执行验证

Checked

获取或设置一个值,该值指示是否已选中RadioButton控件

GroupName

获取或设置单选按钮所属的组名

Text

获取或设置与RadioButton关联的文本标签

TextAlign

获取或设置与RadioButton控件关联的文本标签的对齐方式

Enabled

控件是否启用

ID

获取或设置分配给服务器控件的编程标识符

下面介绍一下RadioButton控件的一些重要属性。

q      GroupName属性

使用GroupName属性指定一组单选按钮,以创建一组互相排斥的控件。如果用户在页面中添加了一组RadioButton控件,可以将所有单选按钮的GroupName属性值设为同一个值,来强制执行在给出的所有选项集中仅有一个处于被选中状态。

q    Checked属性

如果RadioButton控件被选中,则RadioButton控件的Checked属性值为True,否则为False

q    TextAlign属性

RadionButton控件可以通过Text属性指定要在控件中显示的文本。当RadioButton控件的TextAlign属性值为Left时,文本显示在单选按钮的左侧;当RadioButton控件的TextAlign属性值为Right时,文本显示在单选按钮的右侧。

2RadioButton控件常用的事件

RadioButton控件常用的事件是CheckedChanged,当RadioButton控件的选中状态发生改变时引发该事件。

2. 使用RadioButton控件模拟考试系统中的单选题

下面示例通过设置RadioButton控件的GroupName属性值,模拟考试系统中的单选题,并在RadioButton控件的CheckedChanged事件下,将用户选择的答案显示出来。执行程序并选择答案“D”,示例运行结果如图2所示,单击【提交】按钮,将会弹出如图3提示对话框。

 

2  使用RadioButton控件模拟考试系统

      3  对话框

程序实现的主要步骤:

新建一个网站,默认主页为Default.aspxDefault.aspx页面上添加4RadioButton控件、一个Label控件和一个Button按钮控件。属性设置及其用途如表2所示。

2                         Default.aspx页面中控件属性设置及其用途

控件类型

控件名称

主要属性设置

用途

标准/Label控件

Label1

Text属性设置为“?”

显示用户已选择的答案

标准/Button控件

Button1

Text属性设置为“提交”

执行提交功能

标准/RadioButton控件

RadioButton1

Text属性设置为“A:地球是圆的”

显示“A:地球是圆的”文本

AutoPostBack属性设置为True

当单击控件时,自动回发到服务器中

GroupName属性设置为Key

RadioButton控件的组名,强制执行单选操作

TextAlign属性设置为Right

文本显示在单选按钮的右侧

RadioButton2

Text属性设置为“B:地球是长的”

显示“B:地球是长的”文本

AutoPostBack属性设置为True

当单击控件时,自动回发到服务器中

GroupName属性设置为Key

RadioButton控件的组名,强制执行单选操作

TextAlign属性设置为Right

文本显示在单选按钮的右侧

RadioButton3

Text属性设置为“C:地球是方的”

显示“C:地球是方的”文本

AutoPostBack属性设置为True

当单击控件时,自动回发到服务器中

GroupName属性设置为Key

RadioButton控件的组名,强制执行单选操作

TextAlign属性设置为Right

文本显示在单选按钮的右侧

RadioButton4

Text属性设置为“D:地球是椭圆的”

显示“D:地球是椭圆的”文本

AutoPostBack属性设置为True

当单击控件时,自动回发到服务器中

GroupName属性设置为Key

RadioButton控件的组名,强制执行单选操作

TextAlign属性设置为Right

文本显示在单选按钮的右侧

为了使用户将已选择的答案显示在界面上,可以在RadioButton控件的CheckedChanged事件中,使用Checked属性来判断该RadioButton控件是否已被选择,如果已被选择,则将其显示出来。单选按钮RadioButton1CheckedChanged事件代码如下:

protected void RadioButton1_CheckedChanged(object sender, EventArgs e)

    {

        if (RadioButton1.Checked == true)

        {

            this.Label1.Text = "A";

        }

    }

注意:单选按钮RadioButton2RadioButton3RadioButton4控件的CheckedChanged事件代码与RadioButton1控件的CheckedChanged事件代码相似,都是用来判断该单选按钮是否被选中,如果被选择,则将其显示出来。由于篇幅有限,其他单选按钮的CheckedChanged事件代码将不再给出,请读者参见本书光盘。

当用户已选择完答案,可以通过单击【提交】按钮,获取正确答案。【提交】按钮的Click事件代码如下:

protected void Button1_Click(object sender, EventArgs e)

    {

//判断用户是否已选择了答案。如果没有作出选择,将会弹出对话框,提示用户选择答案

        if (RadioButton1.Checked == false && RadioButton2.Checked == false && RadioButton3.Checked == false && RadioButton4.Checked == false)

        {

            Response.Write("<script>alert('请选择答案')</script>");

        }

        else if (RadioButton4.Checked == true)

        {

            Response.Write("<script>alert('正确答案为D,恭喜您,答对了!')</script>");

        }

        else

        {

            Response.Write("<script>alert('正确答案为D,对不起,答错了!')</script>");

        }

    }