Home > Data Access in Web Forms > Data Source Controls Overview > Binding to Data Using a Data Source Control
Introduction to Data Source Controls Filtering Data Using Data Source Controls
In previous versions of ASP.NET, data-bound controls such as the Repeater, DataList and DataGrid were bound to data using the DataSource property, which required writing code to retrieve, sort, filter, page, and modify data.
While this method is still fully supported, data controls since version 2.0 have the added benefit of binding to a data source control using the DataSourceID property instead.
Data source controls greatly expand the capabilities of data-bound controls introduced in version 2.0, such as the GridView, FormView, and DetailsView controls.
When used with data source controls, these controls enable users to easily perform database operations from different data sources, with little or no code.
Basically, to work with data on a Web Forms page in ASP.NET 2.0, you need to do at least the following steps:
- add a data source control, such as a SqlDataSource control
- add a data-bound control, such as a GridView control and bind it to the data source 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 the id of the 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.
The following example shows a GridView control bound to a SqlDataSource control to display data from a database.
Show me
The way in which a data source control retrieves data is determined by the control itself.
The SqlDataSource and AccessDataSource controls select data by running a SQL query specified in the SelectCommand property.
The following example shows a SqlDataSource control that returns data from the Employees table of the Northwind sample database:
Show me
For more information, see Selecting Data Using the SqlDataSource Control.
The ObjectDataSource control reads data by calling the method specified in the SelectMethod property.
For more information, see Introduction to the ObjectDataSource Control.
The XmlDataSource does not allow you to select specific elements from the source XML data. However, you can specify a filter using the XPath property.
For more information, see Filtering Data Using the XmlDataSource Control.
Introduction to Data Source Controls Filtering Data Using Data Source Controls