Programming Microsoft ASP.NET 4 - Dino Esposito [226]
SELECT DISTINCT country, city FROM customers WHERE country=@TheCountry
The @TheCountry parameter is the name of the country/region picked from the drop-down list.
Figure 10-5. A sample Repeater control in action. No predefined list control can generate such a free-form output.
Of all the templates, only ItemTemplate and AlternatingItemTemplate are data-bound, meaning that they are repeated for each item in the data source. You need a mechanism to access public properties on the data item (such as a table record) from within the template. The Eval method takes the name of the property (for example, the name of the table column) and returns the content. You’ll learn more about Eval and <%# … %> code blocks in a moment when we’re discussing data-binding expressions.
The DataList Control
The DataList is a data-bound control that begins where the Repeater ends and terminates a little before the starting point of the DataGrid control. In some unrealistically simple cases, you can even take some code that uses a Repeater, replace the control, and not even notice any difference. The DataList overtakes the Repeater in several respects, mostly in the area of graphical layout. The DataList supports directional rendering, meaning that items can flow horizontally or vertically to match a specified number of columns. Furthermore, it provides facilities to retrieve a key value associated with the current data row and has built-in support for selection and in-place editing.
In addition, the DataList control supports more templates and can fire some extra events beyond those of the Repeater. Data binding and the overall behavior are nearly identical for the Repeater and DataList controls.
In addition to being a naming container, the DataList class implements the IRepeatInfoUser interface. The IRepeatInfoUser interface defines the properties and methods that must be implemented by any list control that repeats a list of items. This interface is also supported by the CheckBoxList and RadioButtonList controls and is the brains behind the RepeatXXX properties you met earlier. Here’s how to rewrite the previous example to get stricter control over the output:
<%# Eval("City") %> <%# Eval("Country")%> <%# CalcTotal() %> citiesWe have customers in the following cities
Note the FooterStyle tag; the DataList also lets you explicitly style the content of each supported template.
Note
The DataList control is deprecated in ASP.NET 4. If you’re building a feature-rich user interface, you might want to take into account more recent view controls, such as the ListView control.
The DataGrid Control
The DataGrid is an extremely versatile data-bound control that is a fixed presence in any real-world ASP.NET application. Although it is fully supported, the DataGrid is pushed into the background by the introduction of a new and much more powerful grid control—the GridView.
The DataGrid control renders a multicolumn, fully templated grid and provides a highly customizable, Microsoft Office Excel–like user interface. In spite of the rather advanced programming interface and the extremely rich set of attributes, the DataGrid simply generates an HTML table with interspersed hyperlinks to provide interactive functionalities such as sorting, paging, selection, and in-place editing.
The DataGrid is a column-based control and supports various types of data-bound columns, including text columns, templated columns, and command columns. You associate the control with a data source using the DataSource property. Just as for other data-bound controls,