SortedList 对象
SortedList对图像包含用键/值得显示的项目。SortedList对图像按顺序或数字顺序自动地对项目进行排序。
通过 Add() 方法向 SortedList 添加项目。通过 TrimToSize() 方法把 SortedList 调整为最小尺寸。
下面的代码创建了一个名为 mycountries 的 SortedList 对象,并添加了四个元素:
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New SortedList
mycountries.Add("N","Norway")
mycountries.Add("S","Sweden")
mycountries.Add("F","France")
mycountries.Add("I","Italy")
end if
end sub
</script>
数据绑定
SortedList 对象可为下一个列表的控件自动生成文本和值:
- asp:单选按钮列表
- asp:复选框列表
- asp:下拉列表
- asp:列表框
为了绑定数据到RadioButtonList控件,首先要在.aspx页面中创建一个RadioButtonList控件(不带任何asp:ListItem元素):
<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>
</body>
</html>
然后添加创建列表的脚本,并绑定列表中的值到 RadioButtonList 控件:
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New SortedList
mycountries.Add("N","Norway")
mycountries.Add("S","Sweden")
mycountries.Add("F","France")
mycountries.Add("I","Italy")
rb.DataSource=mycountries
rb.DataValueField="Key"
rb.DataTextField="Value"
rb.DataBind()
end if
end sub
</script>
<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
</form>
</body>
</html>
然后我们添加一个子示例程序,当用户点击RadioButtonList控件中的某个项目时,该子示例程序会被执行。当某个单选按钮被点击文本出时:label
实例
<script runat="server">
sub Page_Load
if Not Page.IsPostBack then
dim mycountries=New SortedList
mycountries.Add("N","Norway")
mycountries.Add("S","Sweden")
mycountries.Add("F","France")
mycountries.Add("I","Italy")
rb.DataSource=mycountries
rb.DataValueField="Key"
rb.DataTextField="Value"
rb.DataBind()
end if
end sub
sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your favorite country is: " & rb.SelectedItem.Text
end sub
</script>
<html>
<body>
<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>
</body>
</html>