Multi-Record and Single-Value Data Binding for Web Forms Pages
Some Web Forms controls display values from multiple records at once. These controls include the Repeater, DataList, DataGrid, ListBox, CheckBoxList, and RadioButtonList Web server controls, as well as the HtmlSelect control. Other controls, such as Label, TextBox, CheckBox, and Hyperlink controls, display single values, which in data-binding terms means values from a single record.
Data Binding in Multi-Record Controls
To bind multi-record controls, including the Repeater, DataList, DataGrid, ListBox, RadioButtonList, CheckBoxList, and HtmlSelect controls, you set properties that specify where the records are that you want to display. You generally set properties indicating:
- The data source, or the container, containing the records you want to display. A typical data source is a dataset.
- The data member, such as the name of a specific table, if there is more than one bindable member.
- A specific field or column to display. This applies to controls such as the ListBox and HtmlSelect controls that display multiple data items but only one value from each item.
Note You can bind individual properties of multi-record controls to single data values. For example, a DataList control can display multiple records at once, but you might bind its BackColor property to a single data value.
Although you often bind to data in a dataset, you can bind a multi-record control to almost any structure that supports the IEnumerable interface. This includes the following:
- ArrayList objects
- Data readers (objects of type OleDbDataReader and SqlDataReader)
- DataView objects
- DataRow objects
- DataTable objects in datasets
During data binding, the multi-record control loops through the data source, extracting each item in turn. What happens with each data item in turn depends on what type of multi-record control you are working with. A DataGrid control can automatically display all items or a subset of the information based on properties you specify. The Repeater and DataList controls, in contrast, act as containers for controls embedded in their templates (for information see Web Server Controls Templates), which perform their own data binding.
Design-time and Run-time Data Binding
If there is an instance of the data source on your Web Forms page at design time, you can specify binding at design time also. For example, imagine that you want to bind a DataGrid control to a table in a dataset. If there is an instance of the dataset on your Web Forms page, Visual Studio recognizes it as a valid data source and allows you to set the grid's properties to bind to the table. You can bind this way to dataset tables, data views, and other objects on the page.
In many cases, however, the object you want to bind to does not exist until run time. This is true of structures such as an ArrayList or similar object, which you typically instantiate and fill at run time. It is also true for some sources that contain data from a database. For instance, you can get data for the page by using a data command to execute a SQL statement, and then fetch the individual data rows using a data reader. Because you cannot put an instance of the data reader directly onto your page, you must bind to it programmatically at run time.
For more information, see Data Binding Multi-Record Web Server Controls.
Binding to Single Data Values
You can bind individual control properties to virtually any information that can be resolved into a single value appropriate for the property you are binding. A very common scenario, for example, is to bind the Text property of a TextBox control to a data value.
To perform this type of binding, you do not use explicit data binding properties. Instead, you get the data value you want and assign it as a property value of the control.
Note Controls that do not display multiple records — that is, controls other than DataList, DataGrid, and so on — do not include explicit data-binding properties.
Design-time Data Binding
At design time, you can bind a control property to a data value using a "data-binding expression." The expression can reference a row in a dataset table or in a data view, or it can reference a property of another control, or almost anything else. You set a property value to this data-binding expression. When the page runs, the data-binding expression is resolved and the resulting value is assigned to the property. For more information, see Data-Binding Expressions for Web Forms Pages.
If you are using a data-binding expression to bind to data in a dataset, creating a reference to one particular record can be impractical. The dataset does not maintain a pointer or support the concept of a "current record," so there is no implicit way to indicate what record to bind to.
Note This is different from Windows Forms. In Windows Forms, the data-binding architecture built into the form maintains its own set of pointers to records in a dataset, so that controls can be bound to a kind of "current record."
There are several ways to pinpoint the record you want to work with when binding to single data values:
- Create a data view that filters a dataset table to return only one record (for example, by filtering on a unique key). You can then bind the controls to the data view. The data-binding expression for this scenario might look something like the following:
The index value zero (
) indicates that the expression will get data from the first w(and presumably, only) record represented by the data view. For details, see Filtering and Sorting Data Using Data Views.
- Fill a dataset with just one record. However, this might not be an efficient way to use datasets.
Alternatively, you can data bind the control at run time. Doing so allows you to determine programmatically what data row to bind to, which can include executing a SQL statement or stored procedure and binding to the results. For details, see Data Binding Single-Value Web Server Controls at Run Time.
Binding Single-value Controls in Templates
Multi-record controls such as the DataList, Repeater, and the DataGrid controls include templates used to render individual records from the data source. The templates in turn often contain single-value controls such as TextBox and Label controls that are bound to data.
Note For details about templates, see Web Server Controls Templates.
Because the single-value controls in the template can bind to only one value, you need to specify which record to bind to. The parent control makes this possible by exposing two objects that you can use in a data-binding expression:
- The Container object refers to the parent control, which in turn is bound to a data source.
- The DataItem object references the record currently being processed by the parent control.
A data-binding expression for a TextBox control in a DataList control template might look like the following:
For more information, see Data Binding Single-Value Web Server Controls at Design Time.
Introduction to Data Binding in Web Forms Pages | Data-Binding Expressions for Web Forms Pages | Web Server Controls Templates | Data Binding Multi-Record Web Server Controls | Data Binding Single-Value Web Server Controls at Design Time | Data Binding Single-Value Web Server Controls at Run Time | Data Binding HTML Server Controls