Data Sources

As mentioned earlier, data source controls play a crucial role in building data-bound ASP.NET 2.0 Web forms. There are a number of data source controls that ship with ASP.NET 2.0, and they are tailored to different kinds of data. Each data source control implements a common interface called IDataSource, which provides the shared behavior of the data sources as well as defines the interface that derived data source controls must implement to work properly with data-bound controls in Web forms.

A data source control forms a layer of abstraction between a bound control and the data that is being presented. It lets you easily bind multiple controls to the same data source and update all of those controls simply by changing the data in one placeat the data source control. Data source controls encapsulate all the details of connecting to their respective data sources and exposing the underlying data as a collection suitable for binding.

Data source controls, as well as a number of other capabilities in ASP.NET 2.0, follow a design pattern referred to as the provider model. This lets you plug in new capabilities by implementing new components that comply with the interface defined as part of the provider model and integrating them through configuration file entries. As a result, you can define custom data source controls if the built-in controls don't meet your needs.

To use a data source control, you add an instance of one to your Web forms, and set any appropriate properties on the control to drive the way it retrieves and updates data from the underlying source. You then set the DataSourcelD property for a data-bound control, such as a GridView or DropDownList, to the data source control's ID. Each of the ASP.NET databound controls has been extended to include a DataSourcelD property in addition to aDataSource property. You can still set the data source through thdDataSource property, but then you will still be responsible for calling the DataBind method on the control or the page at the appropriate time. When you set the data source through the DataSourcelD, the binding will be automatic as the page is loaded. Let's take a quick look at each of the data source controls that ship with ASP.NET 2.0 and what they are designed for, and then we will get into a couple of examples.

0 0

Post a comment