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

ASP.NET
LinQ学习之旅 从整型数组中找出偶数
解析:如何在 ASP.NET 中下载文件
无废话C#设计模式之九:Proxy
Asp.net中实现从弹出窗口中选择值
.Net中给TreeView控件的节点赋id值
ASP.NET 清除 HTML 标记函数
C#之 VS2008 之 Extension Methods
通过 C# 简化 APM
.Net调用Java webservice访问被拒绝解决方案
C#学习之类的访问修饰符
ASP.NET AJAX中的异步Web Services调用
.Net中生成二维的表格的代码
C#中的两个+(plus)操作符解析
Asp.net 2.0的TreeView客户端个性化控制
直接访问WebBrowser控件中的HTML源码
ASP.NET AJAX中的非同步PageMethod调用
利用C#实现分布式数据库查询
Web Services开发体会和在项目中的教训
开发学习之.Net中PE文件的结构
用C#实现启动另一程序的方法

ASP.NET入门教程:Hashtable对象


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

上一节讲述了ArrayList对象,下面开始讲述Hashtable 对象包含用键/值对表示的项目。键被用作索引,通过搜索其键,可以实现对值的快速搜索。 

Hashtable 对象包含用键/值对表示的项目。

实例

例子 1 - Hashtable RadioButtonList

例子 2 - Hashtable RadiobuttonList

例子 3 - Hashtable DropDownList

创建 Hashtable

Hashtable 对象包含用键/值对表示的项目。键被用作索引,通过搜索其键,可以实现对值的快速搜索。

通过 Add() 方法向 Hashtable 添加项目。看到本信息,说明该文章来源于软晨学习网www.ruanchen.com,如果文章不完整请到软晨学习网ruanchen.com浏览!

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

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then dim mycountries=New Hashtable mycountries.Add("C","China") mycountries.Add("S","Sweden") mycountries.Add("F","France") mycountries.Add("I","Italy")
end if
end sub
</script>

数据绑定

Hashtable 对象可为下面这些控件自动地生成文本和值:

  • 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>

然后添加构建列表的脚本:本信息代表文章来源网页教学ruanchen.com请大家去www.ruanchen.com浏览!

<script runat="server">
sub Page_Load
if Not Page.IsPostBack then dim mycountries=New Hashtable 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 Hashtable 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>

注释:您无法选择添加到 Hashtable 的项目的排序方式。如需对项目进行字母排序或数字排序,请使用 SortedList 对象。