ASP.NET - Hashtable 对象

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

实例

Hashtable RadiobuttonList 1

  1. <script runat="server">
  2. sub Page_Load
  3. if Not Page.IsPostBack then
  4. dim mycountries=New Hashtable
  5. mycountries.Add("C","China")
  6. mycountries.Add("S","Sweden")
  7. mycountries.Add("F","France")
  8. mycountries.Add("I","Italy")
  9. rb.DataSource=mycountries
  10. rb.DataValueField="Key"
  11. rb.DataTextField="Value"
  12. rb.DataBind()
  13. end if
  14. end sub
  15. sub displayMessage(s as Object,e As EventArgs)
  16. lbl1.text="Your favorite country is: " & rb.SelectedItem.Text
  17. end sub
  18. </script>
  19. <html>
  20. <body>
  21. <form runat="server">
  22. <asp:RadioButtonList id="rb" runat="server"
  23. AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
  24. <p><asp:label id="lbl1" runat="server" /></p>
  25. </form>
  26. </body>
  27. </html>

Hashtable RadiobuttonList 2

  1. <script runat="server">
  2. sub Page_Load
  3. if Not Page.IsPostBack then
  4. dim navigate=New Hashtable
  5. navigate.Add("RadioButtonList","/aspnet/control_radiobuttonlist.asp")
  6. navigate.Add("CheckBoxList","/aspnet/control_checkboxlist.asp")
  7. navigate.Add("DropDownList","/aspnet/control_dropdownlist.asp")
  8. navigate.Add("ListBox","/aspnet/control_listbox.asp")
  9. rb.DataSource=navigate
  10. rb.DataValueField="Value"
  11. rb.DataTextField="Key"
  12. rb.DataBind()
  13. end if
  14. end sub
  15. sub navigate(s as Object, e As EventArgs)
  16. response.redirect(rb.SelectedItem.Value)
  17. end sub
  18. </script>
  19. <html>
  20. <body>
  21. <form runat="server">
  22. <asp:RadioButtonList id="rb" runat="server"
  23. AutoPostBack="True" onSelectedIndexChanged="navigate" />
  24. </form>
  25. </body>
  26. </html>

Hashtable DropDownList

  1. <script runat="server">
  2. sub Page_Load
  3. if Not Page.IsPostBack then
  4. dim mycountries=New Hashtable
  5. mycountries.Add("C","China")
  6. mycountries.Add("S","Sweden")
  7. mycountries.Add("F","France")
  8. mycountries.Add("I","Italy")
  9. dd.DataSource=mycountries
  10. dd.DataValueField="Key"
  11. dd.DataTextField="Value"
  12. dd.DataBind()
  13. end if
  14. end sub
  15. sub displayMessage(s as Object,e As EventArgs)
  16. lbl1.text="Your favorite country is: " & dd.SelectedItem.Text
  17. end sub
  18. </script>
  19. <html>
  20. <body>
  21. <form runat="server">
  22. <asp:DropDownList id="dd" runat="server"
  23. AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
  24. <p><asp:label id="lbl1" runat="server" /></p>
  25. </form>
  26. </body>
  27. </html>

创建 Hashtable

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

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

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

  1. <script runat="server">
  2. Sub Page_Load
  3. if Not Page.IsPostBack then
  4. dim mycountries=New Hashtable
  5. mycountries.Add("C","China")
  6. mycountries.Add("S","Sweden")
  7. mycountries.Add("F","France")
  8. mycountries.Add("I","Italy")
  9. end if
  10. end sub
  11. </script>

数据绑定

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

  • asp:RadioButtonList
  • asp:CheckBoxList
  • asp:DropDownList
  • asp:Listbox

如需把数据绑定到某个 RadioButtonList 控件,首先请在一个 .aspx 页面中创建 RadioButtonList 控件(没有任何 asp:ListItem 元素)

  1. <html>
  2. <body>
  3.  
  4. <form runat="server">
  5. <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
  6. </form>
  7.  
  8. </body>
  9. </html>

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

  1. <script runat="server">
  2. sub Page_Load
  3. if Not Page.IsPostBack then
  4. dim mycountries=New Hashtable
  5. mycountries.Add("C","China")
  6. mycountries.Add("S","Sweden")
  7. mycountries.Add("F","France")
  8. mycountries.Add("I","Italy")
  9. rb.DataSource=mycountries
  10. rb.DataValueField="Key"
  11. rb.DataTextField="Value"
  12. rb.DataBind()
  13. end if
  14. end sub
  15. </script>
  16.  
  17. <html>
  18. <body>
  19.  
  20. <form runat="server">
  21. <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" />
  22. </form>
  23.  
  24. </body>
  25. </html>

然后我们添加一个子例程,该例程会在用户点击 RadioButtonList 控件中的某个项目时被执行。当某个单选按钮被点击,label 中会出现一条文本:

  1. <script runat="server">
  2. sub Page_Load
  3. if Not Page.IsPostBack then
  4. dim mycountries=New Hashtable
  5. mycountries.Add("C","China")
  6. mycountries.Add("S","Sweden")
  7. mycountries.Add("F","France")
  8. mycountries.Add("I","Italy")
  9. rb.DataSource=mycountries
  10. rb.DataValueField="Key"
  11. rb.DataTextField="Value"
  12. rb.DataBind()
  13. end if
  14. end sub
  15.  
  16. sub displayMessage(s as Object,e As EventArgs)
  17. lbl1.text="Your favorite country is: " & rb.SelectedItem.Text
  18. end sub
  19. </script>
  20.  
  21. <html>
  22. <body>
  23.  
  24. <form runat="server">
  25. <asp:RadioButtonList id="rb" runat="server"
  26. AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
  27. <p><asp:label id="lbl1" runat="server" /></p>
  28. </form>
  29.  
  30. </body>
  31. </html>

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