Home > Articles > Programming > Windows Programming

Using the ADO.NET's Built-In ASP.NET List Controls

  • Print
  • + Share This
Using ADO.NET to display data on Web forms is incredibly easy. In this lesson you'll see how to use the built-in ASP.NET List controls to quickly add dynamic data to your Web forms.

See all Sams Teach Yourself on InformIT Programming Tutorials.

This chapter is from the book

ADO.NET, a revolutionary step forward in accessing and manipulating data, and its set of supporting display controls, called list controls, are both incredible. For those of you who ever used ADO in conjunction with ASP to dynamically create HTML, you will quickly realize the power and simplicity of list controls. However, don't worry if you've never used ADO; the concepts in this chapter do not require ADO as a prerequisite.

In Hour 9, you learned how to use data list controls with Windows forms in Visual Studio .NET. Throughout the book, you've seen data bound to the DataGrid control. In this hour, you'll see how to use the built-in ASP.NET list controls to display data retrieved from your data source. Specifically, in this hour you'll learn how to

  • Use the Repeater Web control to display a simple menu

  • Use the DataGrid to work with orders

  • Use the DataList to work with products

Some General Notes About List Controls

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:

  1. List controls can bind to any object supporting the IEnumerable interface. For more information on interfaces, see the Coffee Break in this section.

  2. Each list control is geared to display a particular type of data. DataGrids 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.

  3. List controls can all be manipulated as a single entity server-side.

Interfaces? IEnumerable?

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.

  • + Share This
  • 🔖 Save To Your Account