检查radSample.SelecteditemValue,就可以编程确定用户选择了哪个选项,如"option A"。
在下面的例子中,将通过一组单选按钮决定用户在HTML窗体上选择了哪一目的地,并将所做的选择显示给用户。
试一试:使用
(1) 在ch03文件夹中创建TIO-RadioButtonList.ASPx,输入以下代码:
Sub Page_Load()
if Page.IsPostBack then
Message.Text = "You have selected the following: " + radCity.SelectedItem.Value
end if
End Sub
Which city interests you?
(2) 在浏览器中观看页面,如图3-8所示,选择某一城市,并单击Submit Query按钮。
图 3-8
代码的说明
TIORadioButtonList.ASPx页面有3个单选按钮,它们位于ID为radCity的组中。注意每个选项使用了不同的ID和值:
在页面顶部的ASP.NET代码中,我们在Sub Page_Load()和End Sub语句之间使用熟悉的3行代码从窗体返回用户信息。使用与
if Page.IsPostBack then
Message.Text = "You have selected " + radCity.SelectedItem.Value
end if
选择某一单选按钮后,message标签的文本就设置为"You have selected"后跟SelectedItem. Value返回的用户选择。
3.4.6
复选框与单选按钮类似,允许用户从一组按钮中进行多个选择。但是,
一个
如果希望使用多个复选框,可以将它们包含在
下面的例子对上例进行了修改,使用已建立的假日选项,让用户能对目的地进行多个选择。
试一试:使用
(1) 打开TIO-RadioButtonList.ASPx,把它保存为ch03文件夹下的TIO-CheckBoxList.ASPx并修改代码,如下面加底纹的代码所示:
Sub Page_Load()
Dim msg As String = "You have selected the following items:
"
If chkCities.Items(0).Selected Then msg = msg & chkCities.Items(0).Text & "
"
If chkCities.Items(1).Selected Then msg = msg & chkCities.Items(1).Text & "
"
If chkCities.Items(2).Selected Then msg = msg & chkCities.Items(2).Text & "
"
lblCities.Text = msg
End Sub
Which city do you wish to look at hotels for?
(2) 在浏览器中打开TIO-CheckBoxList.ASPx,如图3-9所示,选择多个选项,单击Submit Query按钮。
图 3-9
代码的说明
本例所用代码只对页面做了一些小改动:即将HTML控件改为
ASP.NET代码与前面的TIO-ListBox.ASPx例子相同,但本例所指的是复选框,而不是列表框。语法也作了修改,在msg值的最后添加了城市名。注意可以使用语法msg+=,获得与msg=msg &相同的效果:
Sub Page_Load()
Dim msg As String = "You have selected the following items:
"
If chkCities.Items(0).Selected Then msg = msg & chkCities.Items(0).Text & "
"
If chkCities.Items(1).Selected Then msg = msg & chkCities.Items(1).Text & "
"
If chkCities.Items(2).Selected Then msg = msg & chkCities.Items(2).Text & "
"
lblCities.Text = msg
End Sub
其中包含了一组基本的ASP.NET服务器控件。注意所有的控件都以
在学习其他ASP.NET功能之前,先详细讨论一下如何在ASP.NET中使用变量。之后深入探讨在已使用的逻辑中的控制结构。

