List controls are standard Web controls that serve a single purpose: to display collections of data, such as the results of a database query, on a Windows form or Web form. In ASP, after filling a recordset (comparable to a DataTable object, roughly ) with data, you would manually loop through each record in the resultset and build HTML dynamically. Although this is a very powerful and flexible way of building data-driven pages, often you ended up spending quite a bit of time tweaking the output. Rarely was the code reusable, so it had to be created by hand for each new page.
List controls solve these problems by wrapping up the aforementioned functionality into a single control that can be manipulated as one entity. This makes it much easier to work with and reuse list controls across various pages in your application.
Before looking at individual list controls, it makes sense to briefly discuss what they have in common:
List controls can bind to any object supporting the IEnumerable interface. For more information on interfaces, see the Coffee Break in this section.
Each list control is geared to display a particular type of data. DataGrid s most easily display data that looks good when displayed horizontally in a table. A DataList best displays groupings of data either horizontally or vertically on the page. If you're confused , don't worry ”each of these controls is covered in depth later in this chapter.
List controls can all be manipulated as a single entity server-side.
Typically in the Microsoft programming world, when you see a programming term with a capital "I" as the first character, it refers to an interface. A complete discussion of interfaces is beyond the scope of this book. However, list controls can be much more easily understood by knowing a few things about implementing interfaces.
An "interface" defines a set of properties and methods that an object must support in order to say it "implements" the interface. The IEnumerable interface defines the methods that an object must support in order for ASP.NET to generate the final HTML.
If you had built your own custom server control that contains data that you would like to use to bind directly to a list control, you would implement the IEnumerable interface. This amounts to implementing a single public method named GetEnumerator that returns an object that can iterate through the collection of items in your control.
When attempting to bind data, the list controls try to call the GetEnumerator method of the object they are attempting to bind. If successful, the list controls use the enumerator object returned to loop through all the items in the object getting the data. If the GetEnumerator method isn't present, an exception is returned.
Think of an interface as a set of rules an object must follow by implementing the interface. For more details on implementing IEnumerable or any of the thousands of other interfaces in the Microsoft .NET Framework or COM, please see the Microsoft documentation.