For more complex databinding, some controls include a property named DataSourceID. This new style of databinding is named declarative databinding. Instead of simply iterating through a collection, the declarative databinding classes use a separate Data control to manage data for the databound control. These data managers support the databound controls in implementing standard functionality such as sorting, paging, and editing. Declarative binding greatly simplifies the process of rendering collections. They work by referencing the ID of a DataSource control on the page. .NET includes several of these DataSource controls-including one for XML data, one for Access databases, and one for SQL Server, among others. With declarative databinding, calling DataBind is optional. The control will call DataBind during the Pre[[<img src="images/shy.gif"/>]]Rendering event.
ASP.NET includes a number of controls that support at least simple databinding, while others support declarative databinding as well. These controls include those based upon the ListControl, the CheckBoxList, the RadioButtonList, the DropDownList, and the ListBox. In addition, the more advanced controls include the TreeView, the Menu, the GridView, the DataGrid, the Repeater, the FormView, and the DetailsView.
Here's a rundown of how each control works.
The most common databound controls are those based upon the ListControl base class. These controls include the ListBox, the BulletedList, the RadioButtonList, the CheckBoxList, and the DropDownList. We'll see these controls in detail in a moment. The names are self-explanatory for the most part. They all have direct analogs in Windows desktop programming as well as standard HTML control tags. The ListBox displays a list of strings. The DropDownList is similar to a ComboBox. The RadioButtonList displays a group of mutually exclusive radio buttons. The CheckBoxList displays a column of check box controls.
We saw an example of the TreeView in Tutorial 6. The TreeView control represents hierarchical data. It's perfect for matching up with XML data sources. The TreeView features collapsible nodes that allow users to drill down from abstract data elements into more detailed ones. The TreeView supports declarative databinding.
The Menu control also handles hierarchical databinding. The Menu control gives users the ability to navigate the site in much the same way that menus for desktop applications do. The Menu supports declarative databinding.
The FormView control supports free-form layout for individual controls (such as a TextBox or a ListBox) that render data from a data source. The FormView also supports editing of data in the data source through the controls. The FormView supports declarative databinding.
While ASP.NET 1.x supported only the DataGrid control, ASP.NET 2.0 supports a DataGrid on steroids-the GridView. The GridView control is what it says it is-it renders collections via a grid with individual columns and rows. Each row in the grid represents an individual record in a collection. Each column within that row represents an individual field within the record. While the original DataGrid required you as a developer to manage paging and sorting of data, the GridView control supports automatic paging and sorting. The GridView also supports editing (something that requires hand-coding in the DataGrid). The GridView supports declarative databinding.
Whereas the GridView gives you the whole gestalt of a data source, the DetailsView control is for drilling down to display one record at a time. The DetailsView is often paired with controls such as the ListBox, the DropDownList, or the GridView. Users select the row using one of these controls and the DetailsView shows the associated data. The DetailsView supports declarative databinding.
Whereas the DataGrid and the GridView display the data in a data source using regular rows and columns, the DataList control displays the records in a data source in a format you determine using template controls.
The Repeater control also displays data from a data source in a format you determine (rather than forcing it into rows and columns). The Repeater control uses both raw HTML and server-side controls to display the rows. The Repeater control repeats the format you define for each row.