■21.5选择控件 ■选择控件的作用是让用户从可选项中选取一个或多个选项 包括 Radiobutton和 RadioButton list控件、 CheckBox和 checklist控件、 Listbox和 DropDownlist控件 1. Radio button和 Radiobuttonlist控件 ■单选按钮Web服务器控件分为两类: RadioButton控件和 RadioButtonlist控件。可以使用这些控件定义任意数目的 带标签的单选按钮,并将它们水平或垂直排列。 (1) RadioButton控件 语法格式如下 <asp: Radio Button ID=Radio runat="server Groupname="组名 Text="控件旁显示的文字 oncheckedchanged=Radio Button1_ Checked Changed />
◼ 2.1.5 选择控件 ◼ 选择控件的作用是让用户从可选项中选取一个或多个选项 ,包括RadioButton和RadioButtonList控件、CheckBox和 CheckList控件、ListBox和DropDownList控件。 ◼ 1.RadioButton和RadioButtonList控件 ◼ 单选按钮Web服务器控件分为两类:RadioButton控件和 RadioButtonList控件。可以使用这些控件定义任意数目的 带标签的单选按钮,并将它们水平或垂直排列。 ◼ (1)RadioButton控件 ◼ 语法格式如下: ◼ <asp:RadioButton ID="RadioButton1" runat="server" GroupName="组名" ◼ Text="控件旁显示的文字" oncheckedchanged="RadioButton1_CheckedChanged" … />
表2-12 Radio Button控件的常用属性 GroupName设置 RadioButt控件所属的组名,在同一组内只能有一个控件处于选中状态 hekd设置或获取 RadioButton是否处于被选中状态,tmue表示被选中,fse(默认)表示未被选中 Text 设置或获取显示在控件旁边的说明文字 TextAlign 更改控件旁边的说明文字的方向 AutoPostBack 设置或获取单击时 RadioButton状态是否自动发回服务器 在程序中可以用“控件名称. SelectedItem. Value”获取被选 中按钮的选项值,用“控件名称. Selecteditem.Text”获取被 选中按钮旁显示的文本
◼ 在程序中可以用“控件名称.SelectedItem.Value”获取被选 中按钮的选项值,用“控件名称.SelectedItem.Text”获取被 选中按钮旁显示的文本
使用单个 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; ◼ }