当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > ASP.NET入门教程:SortedList对象

ASP.NET
从ASP.NET的PHP执行速度比较谈起
基于ASP.NET2.0的非HttpModule山寨版MVC框架的实现
ASP.NET 中公有的六种验证控件
无废话C#设计模式之八:Facade
C# 中的 @ 符号的使用及注意事项
保护你的.Net代码:谁动了我的组件?
VS2008与.NET 3.5的JS智能感知和调试
关于 .Net 开发下的分布式缓存设计
在C#3.0中使用扩展方法来扩展接口
C#实现Windows 服务的制作安装和删除
C#3.0中实现隐式类型变量、匿名类型的方法
C#代码与javaScript函数的相互调用
构建 ASP.NET AJAX 开发环境
Windows 窗体的.Net 框架绘图技术
.NET 开发中的一些小技巧集锦
ASP.NET 2.0 创建母版页引来的麻烦
.NET Compact Framework 概述
.NET Compact Framework 2.0 SP1新增功能
专家访谈:从Web 2.0到Web 3.0
.Net里漂浮窗口拖动的实现方法

ASP.NET入门教程:SortedList对象


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-28   浏览: 46 ::
收藏到网摘: n/a

前面讲述了Hashtable对象,下面我们开始讲述SortedList 对象。 

SortedList 对象兼有 ArrayList 和 Hashtable 对象的特性。

实例

例子 1 - SortedList RadioButtonList
例子 2 - SortedList RadiobuttonList
例子 3 - SortedList DropDownList

SortedList 对象

SortedList 对象包含用键/值对表示的项目。SortedList 对象可按照字符顺序或数字顺序自动地对项目进行排序。

通过 Add() 方法向 SortedList 添加项目。SortedList 可通过 TrimToSize() 方法调整为最终尺寸。本信息代表文章来源网页教学ruanchen.com请大家去www.ruanchen.com浏览!

下面的代码创建了一个名为 mycountries 的 SortedList,并添加了四个元素:

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then dim mycountries=New SortedList mycountries.Add("C","China") 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>

然后添加构建列表的脚本:

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then dim mycountries=New SortedList mycountries.Add("C","China") 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("C","China") 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>