asp.net.ph

Skip Navigation LinksHome > Data Access / ADO.NET > Data Source Controls Overview > Binding to Data Using a Data Source Control

Binding to Data Using a Data Source Control

Web Forms Server Controls   Controls You Can Use on Web Forms   ASP.NET Data Source Controls


In previous versions of ASP.NET, data-bound controls such as the DataList and the 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 in 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.

Binding Using the DataSourceID Property

Basically, to work with data on a Web Forms page in ASP.NET 2.0, you need to do at least the following steps:

  1. add a data source control, such as a SqlDataSource control
  2. 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 

Selecting Data

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. The following code example shows an ObjectDataSource control that returns data using the GetAllEmployees method of the EmployeeLogic class:

<%@ Register TagPrefix="aspSample" Namespace="Samples.AspNet.CS" 
   Assembly="Samples.AspNet.CS" %>
<%@ Page language="C#" %>
<html>
<head>
   <title>ObjectDataSource - C# Example</title>
</head>
<body>
   <form runat="server">

      <asp:GridView
         id="GridView1"
         runat="server"
         datasourceid="ObjectDataSource1" />

      <asp:objectdatasource
         id="ObjectDataSource1"
         runat="server"
         selectmethod="GetAllEmployees"
         typename="Samples.AspNet.CS.EmployeeLogic" />

   </form>
</body>
</html>
  C# VB

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.

Modifying Data

The ObjectDataSource and SqlDataSource controls support modifying bound data. For more information, see Modifying Data Using Data Source Controls.

See Also

Sorting Data with Data Source Controls   Using Parameters with Data Source Controls



© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note