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="-- ...

Get Programming ASP .NET now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.