Home > ASP.NET Web Forms > Data Binding in Web Forms > Introduction to Data Binding in Web Forms
Data Binding Expressions for Web Forms Pages
Web Forms enable the display of dynamic content by providing a simple yet powerful means of binding controls to a data source.
This document covers in brief an overview of the ASP.NET data-binding model, and how data binding can effectively be used in ASP.NET Web applications.
Data binding in Web Forms pages is flexible — you can bind any property of any control to any data.
The data source can be almost any type of information available to the page — whether it comes from a database, from an XML document, or from another control or process.
Basically, Web Forms data binding can be done in either of two different ways:
NOTE: The following sections assume you have a fair understanding of ADO.NET and SQL concepts. If not, ADO.NET Primer and Structured Query Language Basics may help to familiarize with the topics.
In the first case ( DataSource ), you provide the logic needed to implement the following steps:
- establish a connection to a data store
- select the needed set of data using a SQL statement
- retrieve the result set into a DataReader or DataSet
- assign the result set as the DataSource of the data-bound control
- explicitly call the DataBind method
In the second case ( DataSourceID ), you provide the logic for the following steps:
- declare a datasource control with a unique ID to contain the data
- set the connectionstring property
- set the selectcommand property using a SQL statement
- set the DataSourceID of the data-bound control to the ID of the datasource control
Each method has its own advantage, and you should use whichever is suited to the particular need at hand. In brief, here are the main differences.
When the DataSourceID property is set, instead of the DataSource property, the data-bound control automatically binds to the data source control at run time.
In addition, using the DataSourceID somehow simplifies database insert, update and delete operations as the datasource control already encompasses the needed logic for these operations internally.
But there are situations where using the DataSourceID is impractical, as when you need to pass parameters to SQL commands before performing database insert, update and delete operations.
You will find out which method is best to use as you progress when developing your application.
There are basically four types of data-binding a control might support:
Some Web Forms controls display values from multiple rows and columns of a data source at once. These include the Repeater, DataList, DataGrid, and GridView controls.
Other controls, such as the Formview and Detailsview controls display values from multiple columns but only from a single row at a time.
List controls, such as the DropDownList, BulletedList, and other list controls, as well as the HtmlSelect control, display values from multiple rows but only from a single column.
Other controls, such as the Label, TextBox, CheckBox, and Hyperlink controls, display only single values from a single record.
NOTE: All controls allow you to bind individual properties to single data values. For example, a DataList control can display multiple records at once, but you can bind its BackColor property to a single data value.
Data Binding Expressions for Web Forms Pages