使用单个 RadioButton控件时,一般方法是:向页面添加 组这样的控件,然后对它们进行分组。可以创建多个不同 的按钮组。向Web窗体页添加 RadioButton控件的方法如下 ■①从工具箱的“标准”选项卡中,将 RadioButton控件拖到 页面上 ■②在“属性”窗口中,通过设置Text属性来指定标题 ③还可以通过设置 TextAlign属性来更改标题的方向。 ■④对要添加到页面上的每个单选按钮重复步骤①~③。 ⑤对添加到页面上的各个 Radiobutton控件进行分组。 ■⑥设置选定的 Radiobutton控件,将控件的 Checked属性设置 为 tue
◼ 使用单个RadioButton控件时,一般方法是:向页面添加一 组这样的控件,然后对它们进行分组。可以创建多个不同 的按钮组。向Web窗体页添加RadioButton控件的方法如下。 ◼ ① 从工具箱的“标准”选项卡中,将RadioButton控件拖到 页面上。 ◼ ② 在“属性”窗口中,通过设置Text属性来指定标题。 ◼ ③ 还可以通过设置TextAlign属性来更改标题的方向。 ◼ ④ 对要添加到页面上的每个单选按钮重复步骤①~③。 ◼ ⑤ 对添加到页面上的各个RadioButton控件进行分组。 ◼ ⑥ 设置选定的RadioButton控件,将控件的Checked属性设置 为true
要确定一组中哪个控件被选中,必须分别测试每个控件,代码示例如 a public void Button1_Click(object sender, System. EventArgs e if ( radiobutton Checked //测试第1个单选按钮是否选中 abell. Text =You selected t Radio Button1. Text else if(Radio Button2 Checked) //测试第2个单选按钮是否选中 Label1Text ="You selected " RadioButton2 Text else if (radiobutton Checked //测试第3个单选按钮是否选中 Labell Text =You selected " Radio 3. Text
◼ 要确定一组中哪个控件被选中,必须分别测试每个控件,代码示例如 下: ◼ public void Button1_Click (object sender, System.EventArgs e) ◼ { ◼ if (RadioButton1.Checked) //测试第1个单选按钮是否选中 ◼ { ◼ Label1.Text = "You selected " + RadioButton1.Text; ◼ } ◼ else if (RadioButton2.Checked) //测试第2个单选按钮是否选中 ◼ { ◼ Label1.Text = "You selected " + RadioButton2.Text; ◼ } ◼ else if (RadioButton3.Checked) //测试第3个单选按钮是否选中 ◼ { ◼ Label1.Text = "You selected " + RadioButton3.Text; ◼ } ◼ }
表2-13 RadioButton控件的常用事件 说明 当用户更改选定项时触发此事件。在默认情况下,此事件不会导致向服务器发送页。但是,可 Checked changed 以通过将 AutoPostBack属性设置为true,来强制该控件立即执行回发 在一般情况下,不需要直接对 Radiobutton控件的选择事件 进行响应,仅当有必要知道用户何时更改了单选按钮组中 的选择内容时,才响应这一事件 当用户选择 RadioButton控件时的响应方法如下: a public void RadioButton_Checked Changed(object sender, System EventArgs e) Label1Text =You selected Radio Button RadioButton.Text:
◼ 在一般情况下,不需要直接对RadioButton控件的选择事件 进行响应,仅当有必要知道用户何时更改了单选按钮组中 的选择内容时,才响应这一事件。 ◼ 当用户选择RadioButton控件时的响应方法如下: ◼ public void RadioButton1_CheckedChanged (object sender, System.EventArgs e) ◼ { ◼ Label1.Text = "You selected Radio Button " + RadioButton1.Text; ◼ }
2) Radiobuttonlist控件 语法格式如下: <asp: Radio ButtonList ID=Radio ButtonList1' runat=server" RepeatDirection="Horizontal onselectedindexchanged=Radio ButtonListl_ SelectedIndexc hanged <asp: ListItem value="选项值1"….>单选按钮旁显示的 文字1</ asp: ListItem> < asp: ListItem Selected="true"…>单选按钮旁显示的文 字2</ asp: Listitem> < asp: Listitem>单选按钮旁显示的文字3</asp: Listitem> </asp: Radio Button List>
◼ (2)RadioButtonList控件 ◼ 语法格式如下: ◼ <asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal" ◼ onselectedindexchanged="RadioButtonList1_SelectedIndexC hanged" … > ◼ <asp:ListItem Value="选项值1" … >单选按钮旁显示的 文字1</asp:ListItem> ◼ <asp:ListItem Selected="true" … >单选按钮旁显示的文 字2</asp:ListItem> ◼ <asp:ListItem>单选按钮旁显示的文字3</asp:ListItem> ◼ … ◼ </asp:RadioButtonList>
表2-14 Radio Button List控件的常用属性 Cellpadding 获取或设置成员控件的边框和内容之间的距离(以像素为单位) Cellspacing 获取或设置相邻表成员控件之间的距离(以像素为单位) Repeatcolumns 设置 RadioButtonlist控件中成员控件显示的列数 RepeatDirection 获取或设置 RadioButtonlist控件中成员控件的显示方向 sed获取或设置列表中被选定项的最小序号索引,如果没有成员被选中,则其值为1 SelectedItem 获取列表控件中索引最小的选定项 Selected value 获取列表控件中选定项的值,或选择列表控件中包含指定值的项 Text 设置显示在控件旁边的说明文字 表2-15 Radio Button list控件的常用事件 说明 SelectedIndex Change当在控件中更改选定项时触发本事件,需要配合 AutoPostBack属性使用