Slide 14.1: Data binding
Slide 14.3: The ArrayList object (cont.)
Home

The ArrayList Object


The ArrayList object is a collection of items containing a single data value. The example below binds a populated ArrayList to an empty RadioButtonList with the following methods:
  1. Items are added to the ArrayList with the Add method. Creates a new ArrayList object named myCountries and four items are added.

  2. By default, an ArrayList object contains 16 entries. An ArrayList can be sized to its final size with the TrimToSize method.

  3. An ArrayList can also be sorted alphabetically or numerically with the Sort method.

demo_arraylist_radio1.aspx

Web
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script language="VB" runat="server">
 Sub Page_Load(s As Object, e As EventArgs) Handles Me.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 xmlns="http://www.w3.org/1999/xhtml" > 
 <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>