當前位置:
首頁 > 知識 > ASP.NET Web Forms-SortedList 對象

ASP.NET Web Forms-SortedList 對象

SortedList 對象結合了 ArrayList 對象和 Hashtable 對象的特性。



ASP.NET Web Forms-SortedList 對象



SortedList 對象

SortedList 對象包含用鍵/值對表示的項目。SortedList 對象按照字母順序或者數字順序自動地對項目進行排序。

通過 Add() 方法向 SortedList 添加項目。通過 TrimToSize() 方法把 SortedList 調整為最終尺寸。

下面的代碼創建了一個名為 mycountries 的 SortedList 對象,並添加了四個元素:

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



ASP.NET Web Forms-SortedList 對象

數據綁定

SortedList 對象可為下列的控制項自動生成文本和值:

  • 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 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 中會出現一行文本:

實例

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

ASP.NET Web Forms-SortedList 對象

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 程序員小新人學習 的精彩文章:

ASP.NET Web Forms-Hashtable 對象
ASP.NET Web Forms-XML 文件
ASP.NET Web Forms-Repeater 控制項
ASP.NET Web Forms-DataList 控制項
ASP.NET Web Forms-維持 ViewState

TAG:程序員小新人學習 |