Programming Microsoft ASP.NET 4 - Dino Esposito [240]
Data Source Components
A data source component is a server control designed to interact with data-bound controls and hide the complexity of the manual data-binding pattern. Data source components not only provide data to controls, they also support data-bound controls in the execution of other common operations such as insertions, deletions, sorting, and updates. Each data source component wraps a particular data provider—relational databases, XML documents, special object models, or custom classes. The support for custom classes means that you can now directly bind your controls to existing classes—for example, classes in your business or data access layer.
Internals of Data Source Controls
A data source control represents one or more named views of data. Each view manages a collection of data. The data associated with a data source control is managed through SQL-like operations such as SELECT, INSERT, DELETE, and COUNT and through capabilities such as sorting and paging. Data source controls come in two flavors: tabular and hierarchical. Tabular controls are described in Table 10-15.
Table 10-15. Tabular Data Source Controls
Class
Description
AccessDataSource
Represents a connection to a Microsoft Access database. It inherits from the SqlDataSource control, but it points to an MDB file and uses the Jet 4.0 OLE DB provider to connect to the database.
EntityDataSource
Allows binding to the results of an Entity Framework query.
LinqDataSource
Allows binding to the results of any supported LINQ provider, including of course LINQ-to-SQL. The control offers properties for you to specify the data context, table name, projection parameters, and where clause.
ObjectDataSource
Allows binding to a custom .NET business object that returns data. The class is expected to follow a specific design pattern and include, for example, a parameterless constructor and methods that behave in a certain way.
SqlDataSource
Represents a connection to an ADO.NET data provider that returns SQL data, including data sources accessible through OLE DB and ODBC. The name of the provider and the connection string are specified through properties.
Note that the SqlDataSource class is not specific to SQL Server. It can connect to any ADO.NET provider that manages relational data. Hierarchical data source controls are listed in Table 10-16.
Table 10-16. Hierarchical Data Source Controls
Class
Description
SiteMapDataSource
Allows binding to any provider that supplies site map information. The default provider supplies site map data through an XML file in the root folder of the application.
XmlDataSource
Allows binding to XML files and strings with or without schema information.
Note that data source controls have no visual rendering. They are implemented as controls to allow for “declarative persistence” (automatic instantiation during the request processing) as a native part of the .aspx source code and to gain access to the page view state.
Data Source Views
A named view is represented by a data source view object—an instance of the DataSourceView class. These classes represent a customized view of data in which special settings for sorting, filtering, and other data operations have been defined. The DataSourceView class is the base class for all views associated with a data source control. The number of views in a data source control depends on the connection string, characteristics, and actual contents of the underlying data source. In ASP.NET, built-in data source controls support only one view, the default view. Table 10-17