Asp tutorial. net ArrayList usage
The first concept we need to understand:
1. What is an ArrayList object?
An ArrayList object is a set of items that contain a single data value.
2. How is an ArrayList object defined?
For more information, see the previous one.
3. ArrayList
There are two examples to illustrate how to use the ArrayList object.
1) ArrayList RadioButtonList
2) ArrayList DropDownList
4. How to create an ArrayList object?
Add () to Add a project to ArrayList. For more information, see
The following code creates a new ArrayList object named mycountries and adds four projects:
<Script runat = "server"> Sub Page_Loadif Not Page. isPostBack then dim mycountries = New ArrayList mycountries. add (www.111cn.net/n86) // mycountries of the rotary kiln. add ("www.111cn.net") // This website is specialized in Zhengzhou website construction mycountries. add ("www.111cn.net") // This website is mycountries. add ("Italy") end ifend sub </script>
Bind data to ArrayList
The ArrayList object can automatically generate text and values to the following controls:
Asp: RadioButtonList
Asp: CheckBoxList
Asp: DropDownList
Asp: Listbox
To bind data to a RadioButtonList control, first create the RadioButtonList control on the. aspx page (note that there is no asp: ListItem element ):
<Html>
<Body>
<Form runat = "server">
<Asp: RadioButtonList id = "rb" runat = "server"/>
</Form>
</Body>
</Html> then add the build List script and bind the values in the list to the RadioButtonList control:
<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> The DataSource attribute of the RadioButtonList control is set to the ArrayList, which defines the data source of the RadioButtonList control. The DataBind () method of the RadioButtonList control binds the RadioButtonList control with the data source.
An ArrayList object is a set of items that contain a single data value.
Instance
Example 1-ArrayList RadioButtonList
Example 2-ArrayList DropDownList
Create ArrayList
An ArrayList object is a set of items that contain a single data value.
Add a project to ArrayList using the Add () method.
The following code creates a new ArrayList object named mycountries and adds four projects:
<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> by default, An ArrayList object contains 16 entries. You can adjust the ArrayList to the final size using the TrimToSize () method:
<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> Using the Sort () method, ArrayList can also be sorted in alphabetical or numerical order:
<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> to Sort data in Reverse order, apply the Reverse () method after the Sort () method:
<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>