导读 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 可以后续的控件自动生成文本和值:

  1. asp:单选按钮列表
  2. asp:复选框列表
  3. asp:下拉列表
  4. 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>

原文来自:https://www.runoob.com/aspnet/aspnet-sortedlist.html

本文地址:https://www.linuxprobe.com/web-forms-sortedlist.html编辑:吴康宁,审核员:逄增宝

Linux命令大全:https://www.linuxcool.com/

Linux系统大全:https://www.linuxdown.com/

红帽认证RHCE考试心得:https://www.rhce.net/