ASP.NET SortedList
asp.net web forms - sortedlist 对象
sortedlist 对象结合了 arraylist 对象和 hashtable 对象的特性。
尝试一下 - 实例
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>
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:radiobuttonlist
- asp:checkboxlist
- asp:dropdownlist
- asp:listbox
为了绑定数据到 radiobuttonlist 控件,首先要在 .aspx 页面中创建一个 radiobuttonlist 控件(不带任何 asp:listitem 元素):
<html>
<body>
<form runat="server">
<asp:radiobuttonlist id="rb" runat="server" autopostback="true" />
</form>
</body>
</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>
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>
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>