共计 1889 个字符,预计需要花费 5 分钟才能阅读完成。
导读 | SortedList 对象结合了 ArrayList 对象和 Hashtable 对象的特性。 |
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 时,会在该控件中添加一个程序的项目时,执行。当单选按钮被点击时,中会行的标签文本:
实例
<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> |
正文完
星哥玩云-微信公众号
