JavaScript Editor Javascript validator     Javascripts

Main Page

Previous Section Next Section

9.1 ArrayList

Later chapters will focus on binding to databases, since that is the common case for serious ASP.NET development. To focus on the mechanics of data-binding, however, this chapter starts simple, by binding controls to an ArrayList rather than to data from a database. Visual Basic .NET, C#, and most other programming languages support the array, an ordered collection of objects, all of the same type. An ArrayList is a .NET Framework collection that acts as an expandable array.

In Chapter 8, you created a drop-down list box that contained the titles of some of my books on programming. The relevant portion of the HTML source appeared as follows:

<ASP:DropDownList id=ddlBooks runat=server>
   <asp:ListItem Value="-- Please Pick A Book --">
      -- Please Pick A Book - 
   <asp:ListItem Value="Programming ASP.NET">
      Programming ASP.NET
   <asp:ListItem Value="Programming C#">Programming C#</asp:ListItem>
   <asp:ListItem Value="Teach Yourself C++ In 21 Days">
      Teach Yourself C++ In 21 Days
   <asp:ListItem Value="Teach Yourself C++ In 24 Hours">
      Teach Yourself C++ In 24 Hours
   <asp:ListItem Value="TY C++ In 10 Minutes">
      TY C++ In 10 Minutes
   <asp:ListItem Value="TY More C++ In 21 Days">
      TY More C++ In 21 Days
   <asp:ListItem Value="C++ Unleashed">C++ Unleashed</asp:ListItem>
   <asp:ListItem Value="C++ From Scratch">C++ From Scratch</asp:ListItem>
   <asp:ListItem Value="XML From Scratch">XML From Scratch</asp:ListItem>
   <asp:ListItem Value="Web Classes FS">Web Classes FS</asp:ListItem>
   <asp:ListItem Value="Beg. OO Analysis &amp; Design">
      Beg. OO Analysis &amp; Design
   <asp:ListItem Value="Clouds To Code">Clouds To Code</asp:ListItem>
   <asp:ListItem Value="CIG Career Computer Programming">
      CIG Career Computer Programming

The HTML source declares the DropDownList object and then declares a ListItem for each book title. Notice that the data here is hard-coded, which is not always optimal, particularly if you are displaying dynamic data. An alternative to declaring each book is to add the books programmatically.

To do so, create the DropDownList in the WebForm, but do not add the items:

      <asp:DropDownList ID="ddlBooks" Runat="server">

Instead of adding ListItem objects, you'll fill the drop-down from a collection, in this case an ArrayList object.

The first step is to create the ArrayList in the Page_Load event handler in the code-behind page (shown here in VB .NET):

Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
   Handles MyBase.Load

   ' declare the list
   Dim bookList As New ArrayList(  )

   ' add the titles
   bookList.Add("Programming ASP.NET")
   bookList.Add("Programming C#")
   bookList.Add("Teach Yourself C++ In 21 Days")
   bookList.Add("Teach Yourself C++ In 24 Hours")
   bookList.Add("TY C++ In 10 Minutes")
   bookList.Add("TY More C++ In 21 Days")
   bookList.Add("C++ Unleashed")
   bookList.Add("C++ From Scratch")
   bookList.Add("XML From Scratch")
   bookList.Add("Web Classes FS")
   bookList.Add("Beg. OO Analysis & Design")
   bookList.Add("Clouds To Code")
   bookList.Add("CIG Career Computer Programming")

This ArrayList object is now ready to use. You can retrieve the titles using normal array syntax, but in this case you want to bind bookList to ddlbooks, the drop-down you created in the WebForm. You start by setting the DataSource property of ddlBooks to your new ArrayList:

ddlBooks.DataSource = bookList

When you set a data source, the data is not bound. You must explicitly bind the data, which you do by calling the DataBind method:

ddlBooks.DataBind(  )

The advantage of requiring explicit binding is that you have complete control over when this action takes place. Since data binding can be an expensive operation (that is, it can take a lot of time and resources), having explicit control over the process can make your program more efficient.

Here is the same code in C#:

protected void Page_Load(object sender, System.EventArgs e)
   // create the array list
   ArrayList bookList = new ArrayList(  );

   // add all the books
   bookList.Add("Programming ASP.NET");
   bookList.Add("Programming C#");
   bookList.Add("Teach Yourself C++ In 21 Days");
   bookList.Add("Teach Yourself C++ In 24 Hours");
   bookList.Add("TY C++ In 10 Minutes");
   bookList.Add("TY More C++ In 21 Days");
   bookList.Add("C++ Unleashed");
   bookList.Add("C++ From Scratch");
   bookList.Add("XML From Scratch");
   bookList.Add("Web Classes FS");
   bookList.Add("Beg. OO Analysis & Design");
   bookList.Add("Clouds To Code");
   bookList.Add("CIG Career Computer Programming");

   // set the data source

   // bind to the data
   ddlBooks.DataBind(  );

You will typically bind your data at two points while your program is running:

  • When the Page.Load event procedure has fired, to initialize the values

  • Again any time the data is changed

Once the data is bound, the drop-down list is filled with the contents of the array, as shown in Figure 9-1.

Figure 9-1. ArrayList bound to a list box
    Previous Section Next Section

    JavaScript Editor Javascript validator     Javascripts