asp.net ArrayList用法

來源:互聯網
上載者:User

asp教程.net ArrayList用法

我們要瞭解的第一個概念:
1、什麼是ArrayList對象?
     ArrayList 對象是包含單一資料值的項目的集合。
2、ArrayList對象是如何定義的?
     同1,詳情請看上一條。
3、ArrayList執行個體講解
    今天給大家講的有兩個例子來說明ArrayList對象的使用方法。
    1)ArrayList RadioButtonList
    2)ArrayList DropDownList
4、怎麼建立ArrayList對象?
    是通過Add() 來向ArrayList來添加項目的,詳情請繼續往下看
    下面的代碼建立了一個新的 ArrayList 對象,名為 mycountries,並添加了四個項目:

<script runat="server">Sub Page_Loadif Not Page.IsPostBack then  dim mycountries=New ArrayList  mycountries.Add(www.111cn.net/n86) //該網做是做迴轉窯的mycountries.Add("www.111cn.net") //該網站是專業鄭州網站建設  mycountries.Add("www.111cn.net") //該網站是滎陽人才網  mycountries.Add("Italy")end ifend sub</script>

資料繫結到 ArrayList
ArrayList 對象可向下面這些控制項自動地產生文本和值:

asp:RadioButtonList
asp:CheckBoxList
asp:DropDownList
asp:Listbox
如需把資料繫結到一個 RadioButtonList 控制項,首先請在一個 .aspx 頁面中建立 RadioButtonList 控制項(請注意,沒有任何 asp:ListItem 元素):

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>

</body>
</html>然後添加構建列表的指令碼,並把列表中的值綁定到該 RadioButtonList 控制項:

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("China")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
  mycountries.Sort()
  rb.DataSource=mycountries
  rb.DataBind()
end if
end sub
</script>

<html>
<body>

<form runat="server">
<asp:RadioButtonList id="rb" runat="server" />
</form>

</body>
</html>RadioButtonList 控制項的 DataSource 屬性被設定為該 ArrayList,它定義了這個 RadioButtonList 控制項的資料來源。RadioButtonList 控制項的 DataBind() 方法把 RadioButtonList 控制項與資料來源綁定在一起

 

ArrayList 對象是包含單一資料值的項目的集合。
執行個體
例子 1 - ArrayList RadioButtonList

例子 2 - ArrayList DropDownList
建立 ArrayList
ArrayList 對象是包含單一資料值的項目的集合。

通過 Add() 方法向 ArrayList 添加項目。

下面的代碼建立了一個新的 ArrayList 對象,名為 mycountries,並添加了四個項目:

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("China")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
end if
end sub
</script>預設地,一個 ArrayList 對象包含 16 個條目。可通過 TrimToSize() 方法把 ArrayList 調整為最終尺寸:

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("China")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
end if
end sub
</script>通過 Sort() 方法,ArrayList 也能夠按照字母順序或者數字順序進行排序:

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("China")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
  mycountries.Sort()
end if
end sub
</script>要實現顛倒的排序,請在 Sort() 方法後應用 Reverse() 方法:

<script runat="server">
Sub Page_Load
if Not Page.IsPostBack then
  dim mycountries=New ArrayList
  mycountries.Add("China")
  mycountries.Add("Sweden")
  mycountries.Add("France")
  mycountries.Add("Italy")
  mycountries.TrimToSize()
  mycountries.Sort()
  mycountries.Reverse()
end if
end sub
</script>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.