_.NET概论及软件使用_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

A.NET入门教程:Hashtable对象.

作者:黑客防线网安ASP维护基地 来源:黑客防线网安ASP维护基地 浏览次数:0

本篇关键词:对象教程入门 

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

实例
例子 1 - Hashtable RadioButtonList

例子 2 - Hashtable RadiobuttonList

例子 3 - Hashtable DropDownList

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

通过 Add() 方法向 Hashtable 添加项目。

下面的代码创建一个名为 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>然后添加构建列表的脚本:

<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 对象。

    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-14098-1.html
网站维护教程更新时间:2012-03-30 05:26:23  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer