ASP.NET - SortedList 对象

SortedList 对象兼有 ArrayList 和 Hashtable 对象的特性。

实例

SortedList RadiobuttonList 1

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

SortedList RadiobuttonList 2

  1. <script runat="server">
  2. sub Page_Load
  3. if Not Page.IsPostBack then
  4. dim navigate=New SortedList
  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>

SortedList DropDownList

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

SortedList 对象

SortedList 对象包含用键/值对表示的项目。SortedList 对象可按照字符顺序或数字顺序自动地对项目进行排序。

通过 Add() 方法向 SortedList 添加项目。SortedList 可通过 TrimToSize() 方法调整为最终尺寸。

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

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

数据绑定

SortedList 对象可自动地为下面的控件生成文本和值:

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