ASP.NET 教程
1. ASP.NET 教程 2. ASP.NET 3. ASP.NET Web Pages 教程 4. ASP.NET Web Pages Razor 5. ASP.NET Web Pages 布局 6. ASP.NET Web Pages 文件夹 7. ASP.NET Web Pages 全局文件 8. ASP.NET Web Pages HTML 表单 9. ASP.NET Web Pages 对象 10. ASP.NET Web Pages 文件 11. ASP.NET Web Pages 帮助器 12. ASP.NET Web Pages WebGrid 13. ASP.NET Web Pages 图表 14. ASP.NET Web Pages Email 15. ASP.NET Web Pages PHP 16. ASP.NET Web Pages – 发布 17. ASP.NET Web 的 C# 和 VB 实例 18. ASP.NET Web Pages 类参考手册 19. ASP.NET Web Pages WebSecurity 参考手册 20. ASP.NET Web Pages Database 参考手册 21. ASP.NET Web Pages WebMail 参考手册 22. ASP.NET WebPages 帮助器参考手册 23. ASP.NET Razor 标记 24. ASP.NET Razor 语法 25. ASP.NET Razor C# 变量 26. ASP.NET Razor C# 循环和数组 27. ASP.NET Razor C# 逻辑 28. ASP.NET Razor VB 变量 29. ASP.NET Razor VB 循环和数组 30. ASP.NET Razor VB 逻辑 31. ASP.NET MVC 简介 32. ASP.NET MVC Web 应用程序 33. ASP.NET MVC 文件夹 34. ASP.NET MVC 页面和布局 35. ASP.NET MVC 控制器 36. ASP.NET MVC 视图 37. ASP.NET MVC 数据库 38. ASP.NET MVC 模型 39. ASP.NET MVC 安全 40. ASP.NET MVC HTML 帮助器 41. ASP.NET MVC – 发布 42. ASP.NET MVC 参考手册 43. ASP.NET Web Forms 教程 44. ASP.NET Web 页面 45. ASP.NET 服务器控件 46. ASP.NET 事件句柄 47. ASP.NET Web 表单 48. ASP.NET ViewState 49. ASP.NET TextBox 控件 50. ASP.NET Button 控件 51. ASP.NET 数据绑定 52. ASP.NET ArrayList 53. ASP.NET Hashtable 54. ASP.NET SortedList 55. ASP.NET XML 数据绑定 56. ASP.NET Repeater 控件 57. ASP.NET DataList 控件 58. ASP.NET 数据库连接 59. ASP.NET 母版页 60. ASP.NET 导航 61. ASP.NET 实例 62. ASP.NET HTML 服务器控件 63. ASP.NET Web 服务器控件 64. ASP.NET Validation 服务器控件

ASP.NET Hashtable

ASP.NET Web Forms - Hashtable 对象


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


55面试教程网 - 实例

Hashtable RadiobuttonList 1

Hashtable RadiobuttonList 2

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("N","Norway")
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>

然后添加创建列表的脚本,并且绑定列表中的值到 RadioButtonList 控件:

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

演示实例 »

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