VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 网站开发 > ASP.net 4.0教程 >
  • asp.net教程之Web Forms - 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("N","Norway")
  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,As EventArgs)
  17. lbl1.text="Your favorite country is: " & rb.SelectedItem.Text
  18. end sub
  19. </script>
  20.  
  21. <!DOCTYPE html>
  22. <html>
  23. <body>
  24.  
  25. <form runat="server">
  26. <asp:RadioButtonList id="rb" runat="server"
  27. AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
  28. <p><asp:label id="lbl1" runat="server" /></p>
  29. </form>
  30.  
  31. </body>
  32. </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","control_radiobuttonlist.asp")
  6.    navigate.Add("CheckBoxList","control_checkboxlist.asp")
  7.    navigate.Add("DropDownList","control_dropdownlist.asp")
  8.    navigate.Add("ListBox","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.  
  16. sub navigate(s as Object, e As EventArgs)
  17. response.redirect(rb.SelectedItem.Value)
  18. end sub
  19. </script>
  20.  
  21. <!DOCTYPE html>
  22. <html>
  23. <body>
  24.  
  25. <form runat="server">
  26. <asp:RadioButtonList id="rb" runat="server"
  27. AutoPostBack="True" onSelectedIndexChanged="navigate" />
  28. </form>
  29.  
  30. </body>
  31. </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("N","Norway")
  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.  
  16. sub displayMessage(s as Object,As EventArgs)
  17. lbl1.text="Your favorite country is: " & dd.SelectedItem.Text
  18. end sub
  19. </script>
  20.  
  21. <!DOCTYPE html>
  22. <html>
  23. <body>
  24.  
  25. <form runat="server">
  26. <asp:DropDownList id="dd" runat="server"
  27. AutoPostBack="True" onSelectedIndexChanged="displayMessage" />
  28. <p><asp:label id="lbl1" runat="server" /></p>
  29. </form>
  30.  
  31. </body>
  32. </html>
复制

SortedList 对象

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

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

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


		
  1. <script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New SortedList mycountries.Add("N","Norway") mycountries.Add("S","Sweden") mycountries.Add("F","France") mycountries.Add("I","Italy") end if end sub </script>
复制

数据绑定

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

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

为了绑定数据到 RadioButtonList 控件,首先要在 .aspx 页面中创建一个 RadioButtonList 控件(不带任何 asp:ListItem 元素):


		
  1. <html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" AutoPostBack="True" /> </form> </body> </html>
复制

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


		
  1. <script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New SortedList 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 中会出现一行文本:

实例


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


相关教程