Data-Bound Web Server Controls
Data-bound web server controls are controls that can be bound to a data source control to make it easy to display and modify data in your web application. All of these controls provide a variety of properties that you can set to control the appearance of the UI that they generate. For scenarios where you need greater control over a control's UI or how it processes input, some of these controls let you specify the generated HTML directly using templates. A template is a block of HTML markup that includes special variables that you use to specify where and how the bound data is to be displayed. When the control is rendered, the variables are replaced with actual data and the HTML is rendered to the browser.
For example, a data-bound control such as a DetailsView control can bind to a set of results such as a table of employees containing each employee's name, address, job title, and so on. Within the DetailsView control, you can bind Label controls to single data values such as the name or address field to create the data layout in the page.
In addition to enabling you to bind the control to a set of data results, data-bound controls enable you to customize the layout of the control using templates. They also provide a convenient model for handling and canceling events.
This topic discusses how data web server controls bind to data as well as the data-bound controls that are included with ASP.NET.
Binding a Data-bound Web Server Control to a Data Source
You can work with a data-bound control by binding it to a data source control such as an ObjectDataSource or SqlDataSource control. The data source control connects to a data source such as a database or middle-tier object and then retrieves or updates data. The data-bound control can then use this data. To perform the binding, you set the data-bound control's DataSourceID property to point to a data source control. When a data-bound control is bound to a data source control, little or no additional code is required for data operations, because the data-bound control can automatically take advantage of the data services provided by the data source control.
In ASP.NET version 1.0 and version 1.1, data-bound controls were bound to data using the DataSource property and required you to write code to handle operations such as displaying, paging, sorting, editing, and deleting data. Although you can still bind controls to data by using the DataSource property (and use existing code), in ASP.NET version 2.0 and later you can perform binding by using the DataSourceID property instead.
For more information about data source controls, see Data Source Web Server Controls.
The GridView control displays data as a table and provides the capability to sort columns, page through data, and edit or delete a single record.
For more information, see GridView Web Server Control Overview and Comparing the GridView and DataGrid Web Server Controls.
The ListView control displays data from a data source in a format that you define using templates. The template contains the formatting, controls, and binding expressions that are used to lay out the data. The ListView control is useful for data in any repeating structure, similar to the DataList and Repeater controls. However, unlike the DataList and Repeater controls, the ListView control implicitly supports edit, insert, and delete operations, as well as sorting and paging functionality.
For more information, see ListView Web Server Control Overview.
The DetailsView control renders a single record at a time as a table and provides the capability to page through multiple records, as well as to insert, update, and delete records. The DetailsView control is often used in master-detail scenarios where the selected record in a master control such as a GridView control determines the record displayed by the DetailsView control.
For more information, see DetailsView Web Server Control Overview.
The FormView control renders a single record at a time from a data source and provides the capability to page through multiple records, as well as to insert, update, and delete records, similar to the DetailsView control. However, the difference between the FormView and the DetailsView controls is that the DetailsView control uses a table-based layout where each field of the data record is displayed as a row in the control. In contrast, the FormView control does not specify a pre-defined layout for displaying a record. Instead, you create templates that contain controls to display individual fields from the record. The template contains the formatting, controls, and binding expressions used to lay out the form. You can specify whether the FormView control renders content using tables by setting the RenderTable property to true.
For more information, see FormView Web Server Control Overview.
The Repeater control renders a read-only list from a set of records returned from a data source. Like the FormView control, the Repeater control does not specify a built-in layout. Instead you create the layout for the Repeater control using templates.
For more information, see Repeater Web Server Control Overview.
The Chart control renders a chart.
For more information, see.
Data-bound UI controls that require you to write templates for them include FormView control for creating a form that accepts input and the ListView control for displaying a list. Controls that offer the option of writing templates when you need them include the GridView control for displaying data in tables and the DetailsView control for displaying one record at a time. Data-bound UI controls that do not let you write templates include the BulletedList, CheckBoxList, DropDownList, ListBox, and RadioButtonList controls. For more information and a complete list of data-bound controls, see Data Web Server Controls.
ASP.NET includes the data-bound controls described in the following table.
Renders advertisements on a page as an image that users can click to go to a URL associated with the advertisement.
For more information, see AdRotator Web Server Control.
Displays one record at a time in a tabular layout and enables you to edit, delete, and insert records. You can also page through multiple records.
For more information see DetailsView Web Server Control.
For more information, see FormView Web Server Control.
Displays data in a table and includes support for editing, updating, deleting, sorting, and paging data without requiring code.
For more information, see GridView Web Server Control.
For more information, see ListView Web Server Control.
Renders data in a hierarchical dynamic menu that can include submenus.
For more information, see Menu Web Server Control.
Renders data in a list. Each item is rendered using an item template that you define.
For more information, see Repeater Web Server Control.
Renders data in a hierarchical tree of expandable nodes.
For more information, see TreeView Web Server Control.
Enabling Dynamic Data for Data-Bound Controls
You can use Dynamic Data features in existing ASP.NET Web applications that do not use scaffolding by enabling Dynamic Data for individual data-bound controls. Dynamic Data provides the presentation and data layer support for rendering these controls. When you enable Dynamic Data for data-bound controls, you get the following benefits:
Setting default values for data fields. Dynamic Data enables you to provide default values at run time for fields in a data control.
Interacting with the database without creating and registering a data model. Data-bound controls that support Dynamic Data can interact with the database by using any data source, without scaffolding. This avoids having to configure a LINQ to SQL or Entity Framework data model and use the LinqDataSource and EntityDataSource controls.
Automatically validating the data that is entered by the user without writing any code.
When you use this approach, Dynamic Data performs all the tasks for accessing and rendering data. You must write a small amount of code, and you do have limited control over Dynamic Data operations.
You can enable dynamic behavior in ASP.NET data-bound controls by using methods in the DynamicDataExtensions class. For more information, see Walkthrough: Enabling Dynamic Data in ASP.NET Data-Bound Controls.