Chapter 9. Data Binding

Nearly every ASP.NET application displays data of some sort, either from a database or from other data sources. Data binding allows you to create a relationship between a control (such as a list box or data grid) and a source of data (such as SQL Server). ASP.NET takes care of the details of displaying the data in your control.

You can bind to any data source, including such simple sources as properties, expressions, or the result of a method call, and such complex sources as arrays, collections, and databases. For controls that display a collection, such as a list box or data grid, you must bind to a source which implements the ICollection interface. This allows ASP.NET to iterate the collection and display each member in turn.

ArrayList

Later chapters will focus on binding to databases, since that is the common case for serious ASP.NET development. In order 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 language 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 the previous chapter, 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>
<asp:ListItem Value="Programming ASP.NET">Programming 
   ASP.NET</asp:ListItem>
<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>
<asp:ListItem Value="Teach Yourself C++ In 24 Hours">Teach Yourself C++ In 
   24 Hours</asp:ListItem>
<asp:ListItem Value="TY C++ In 10 Minutes">TY C++ In 10 
   Minutes</asp:ListItem>
<asp:ListItem Value="TY More C++ In 21 Days">TY More C++ In 21 
   Days</asp:ListItem>
<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>
<asp:ListItem Value="Clouds To Code">Clouds To Code</asp:ListItem>
<asp:ListItem Value="CIG Career Computer Programming">CIG Career Computer 
   Programming</asp:ListItem>
               </ASP:DropDownList>

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:

<tr>
   <td>
      Book
   </td>
   <td>
      <asp:DropDownList ID="ddlBooks" Runat="server">
      </asp:DropDownList>
   </td>
</tr>

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:

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
   ddlBooks.DataSource=bookList;

   // 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.

ArrayList bound to a list box

Figure 9-1. ArrayList bound to a list box

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.223.190