ASP.NET - The ArrayList Object
2007-01-19 16:41
204 查看
The ArrayList object is a collection of items containing a single data value.
ASPX Source:
Output Result:
if you click the button "france",it will show:
Example 2 - ArrayList DropDownList
ASPX Source:
Output Result:
if you click the checkbox "Frace",it will show:
Items are added to the ArrayList with the Add() method.
The following code creates a new ArrayList object named mycountries and four items are added:
By default, an ArrayList object contains 16 entries. An ArrayList can be sized to its final size with the TrimToSize() method:
An ArrayList can also be sorted alphabetically or numerically with the Sort() method:
To sort in reverse order, apply the Reverse() method after the Sort() method:
asp:RadioButtonList
asp:CheckBoxList
asp:DropDownList
asp:Listbox
To bind data to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page:
Then add the script that builds the list and binds the values in the list to the RadioButtonList control:
The DataSource property of the RadioButtonList control is set to the ArrayList and it defines the data source of the RadioButtonList control. The DataBind() method of the RadioButtonList control binds the data source with the RadioButtonList control.
Note: The data values are used as both the Text and Value properties for the control. To add Values that are different from the Text, use either the Hashtable object or the SortedList object.
Examples
Example 1 - ArrayList RadioButtonListASPX Source:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() mycountries.Sort() rb.DataSource=mycountries 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> |
Output Result:
|
if you click the button "france",it will show:
|
ASPX Source:
<script runat="server"> sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() mycountries.Sort() dd.DataSource=mycountries dd.DataBind() end if end sub sub displayMessage(s as Object,e As EventArgs) lbl1.text="Your favorite country is: " & dd.SelectedItem.Text end sub </script> <html> <body> <form runat="server"> <asp:DropDownList id="dd" runat="server" AutoPostBack="True" onSelectedIndexChanged="displayMessage" /> <p><asp:label id="lbl1" runat="server" /></p> </form> </body> </html> |
Output Result:
France Italy Norway Sweden |
if you click the checkbox "Frace",it will show:
France Italy Norway Sweden Your favorite country is: France |
Create an ArrayList
The ArrayList object is a collection of items containing a single data value.Items are added to the ArrayList with the Add() method.
The following code creates a new ArrayList object named mycountries and four items are added:
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") end if end sub </script> |
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() end if end sub </script> |
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() mycountries.Sort() end if end sub </script> |
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("Norway") mycountries.Add("Sweden") mycountries.Add("France") mycountries.Add("Italy") mycountries.TrimToSize() mycountries.Sort() mycountries.Reverse() end if end sub </script> |
Data Binding to an ArrayList
An ArrayList object may automatically generate the text and values to the following controls:asp:RadioButtonList
asp:CheckBoxList
asp:DropDownList
asp:Listbox
To bind data to a RadioButtonList control, first create a RadioButtonList control (without any asp:ListItem elements) in an .aspx page:
<html> <body> <form runat="server"> <asp:RadioButtonList id="rb" runat="server" /> </form> </body> </html> |
<script runat="server"> Sub Page_Load if Not Page.IsPostBack then dim mycountries=New ArrayList mycountries.Add("Norway") 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> |
Note: The data values are used as both the Text and Value properties for the control. To add Values that are different from the Text, use either the Hashtable object or the SortedList object.
相关文章推荐
- ASP.NET4.0错误:Operation is not valid due to the current state of the object
- ASP.NET - The SortedList Object
- 【ASP.NET Step by Step】之六 Programmatically Setting the ObjectDataSource's Parameter Values
- ASP.NET - The Hashtable Object
- Understanding the Session Object in ASP.NET 2.0
- 解决asp.net error: Operation is not valid due to the current state of the object
- The ASP.NET Object Model
- 【ASP.NET Step by Step】之四 Displaying Data With the ObjectDataSource
- Using The Amazon Web Service From ASP.NET
- ASP.NET NuGet to install the mvc 5.2.2
- 从底层角度看ASP.NET-A low-level Look at the ASP.NET Architecture
- How to Pop Open an image window that resizes to fit the image in ASP.net 1.x and 2.0 Beta 1
- Fw:Managing View State in ASP.NET 4 Using the New ViewStateMode Property
- .NET浏览ASP.NET页面时出现This application is currently offline. To enable the application, remove...错误的解决办法
- 4 ways to send a PDF file to the IE Client in ASP.NET 2.0
- [C#]JSONObject解析json,C# asp.net JSON解析
- A low-level Look at the ASP.NET Architecture
- Tip/Trick: Implement 'Donut Caching' with the ASP.NET 2.0 Output Cache Substitution Feature f8e2
- You cannot access the client's file system using the FileSystemObject in your ASP code
- My thesis--a chatting room based on ASP.NET