IT教程 | 加入收藏 | 简体 | 繁体
Asp.net教程:Asp教程VbScript教程Asp.net教程
当前位置: 首页 > Asp+Net > Asp.net教程

ASP.NET Hashtable 对象 实例教程

2012-11-13来源:[www.121ask.com]热度:0℃ 【字体:

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

实例

Hashtable RadiobuttonList 1实例:

<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 RadiobuttonList 2实例:

<script  runat="server">
sub Page_Load
if Not Page.IsPostBack then
   dim navigate=New Hashtable
   navigate.Add("RadioButtonList","/aspnet/control_radiobuttonlist.asp")
   navigate.Add("CheckBoxList","/aspnet/control_checkboxlist.asp")
   navigate.Add("DropDownList","/aspnet/control_dropdownlist.asp")
   navigate.Add("ListBox","/aspnet/control_listbox.asp")
   rb.DataSource=navigate
   rb.DataValueField="Value"
   rb.DataTextField="Key"
   rb.DataBind()
end if
end sub

sub navigate(s as Object, e As EventArgs)
response.redirect(rb.SelectedItem.Value)
end sub
</script>

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server"
AutoPostBack="True" onSelectedIndexChanged="navigate" />
</form>

</body>
</html>
 

Hashtable DropDownList实例:

<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")
   dd.DataSource=mycountries
   dd.DataValueField="Key"
   dd.DataTextField="Value"
   dd.DataBind()
end if
end sub

sub displayMessage(s as Object,e As EventArgs)
lbl1.text="Your favorite country is: " & dd.SelectedItem.Text
end sub
</script>

<html>
<body>

<form runat="server">
<asp:DropDownList id="dd" runat="server"
AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
<p><asp:label id="lbl1" runat="server" /></p>
</form>

</body>
</html>
[NextPage]

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

 

以上IT文章《ASP.NET Hashtable 对象 实例教程》由名客技术网[www.121ask.com]提供阅读!
推广信息
推广信息
名客技术网是一个免费为广大IT技术人员提供IT教程,绿色软件,asp/php/jsp/java/net等实例视频教程,CMS教程,破解软件下载以及学习相关源码、知识的IT网站。
名客技术网如果无意之中侵犯了您的版权,请来电告知,本站将在3个工作日内删除 QQ:175352796
Copyright 2011-2017 Powered by 121ASK.COM, All Rights Reserved.
备案号:鄂ICP备11013833号-3