asp.net.ph

Skip Navigation LinksHome > ASP.NET Web Forms > Data Binding in Web Forms > Introduction to 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.

Overview

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:

  1. establish a connection to a data store
  2. select the needed set of data using a SQL statement
  3. retrieve the result set into a DataReader or DataSet
  4. assign the result set as the DataSource of the data-bound control
  5. explicitly call the DataBind method
DataSource Example
Run Sample | View Source
DataSource Example
Run Sample | View Source

In the second case ( DataSourceID ), you provide the logic for the following steps:

  1. declare a datasource control with a unique ID to contain the data
  2. set the connectionstring property
  3. set the selectcommand property using a SQL statement
  4. set the DataSourceID of the data-bound control to the ID of the datasource control
DataSourceID Example
Run Sample | View Source
DataSource and DataSourceID Example
Run Sample | View Source

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.

Multiple Records, Multiple Values, Single Record, and Single-Value Controls

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.

DataBinding in Repeater
Run Sample | View Source
DataBinding in DataList
Run Sample | View Source
DataBinding in DataGrid
Run Sample | View Source
DataBinding in Gridview
Run Sample | View Source

Other controls, such as the Formview and Detailsview controls display values from multiple columns but only from a single row at a time.

DataBinding in FormView
Run Sample | View Source
DataBinding in DetailsView
Run Sample | View Source

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.

DataBinding a DropDownList
Run Sample | View Source
DataBinding a BulletedList
Run Sample | View Source
DataBinding an HtmlSelect
Run Sample | View Source

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.

See Also

Data Binding Expressions for Web Forms Pages



© 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