This documentation is archived and is not being maintained.

Introduction to Data Binding in Web Forms Pages

Visual Studio .NET 2003

As in most types of forms, Web Forms pages allow you to display information by binding controls to a source of data. However, due to the nature of Web Forms pages themselves and to the architecture of Web programming, data binding in Web Forms pages is somewhat different than it is in other, more traditional forms such as Windows Forms.

This topic provides background information on data binding in Web Forms pages, how you work with data binding in your Web applications, and where you can find more information about data binding and data access in Web Forms pages.

Web Forms Data Binding: Read-Only

The data-binding architecture of Web Forms pages is based on the idea that the majority of data access in a Web application is read-only — that is, that pages frequently display information from a data store, but much less frequently need to write data back to the store. There are many examples of pages that present read-only data:

  • A search results page for other Web pages, products, and so on. Information in a search results page is often presented as links to other pages, and rarely is there a need for users to enter data to be saved in a database.
  • A page that provide sports scores, weather information, and so on.
  • A portal page. Many portal pages (such as the MSN home page) allow users to customize the page by choosing from various types of information to display — news, financial information, entertainment options, and so on. The information itself is often provided to the page as XML data that can be transformed according to user preferences.
  • A page that provides account and financial transaction information to a bank or brokerage customer.

Because the Web Forms data-binding architecture does not perform updates — that is, it does not write data from the control back to the data source — you must provide this logic. The logic for performing updates in Web Forms pages varies considerably according to a variety of factors:

  • What the data source is.
  • Whether you are caching data by using a dataset.
  • What type of control you are working with — for example, whether the control displays multiple records, as in a grid, or single values, as in a text box.
  • What type of concurrency control you want to use.

Because of the variables associated with updating from Web Forms pages, you implement your own update logic to match the specific requirements of your Web Forms page and application. For more information, see Dataset Updates in Visual Studio .NET.

Binding Properties

Data binding in Web Forms pages is flexible — you can bind any property of a control to data. In contrast, in traditional data binding, you typically bind the display property (for example, the text property of a text box) to the data source.

In Web Forms pages, your data source can be almost any type of information available to the page — whether it comes from a database, from an XML document, from another control or process, or even from a calculation you perform yourself.

Web Forms data binding, moreover, allows you to assign this data to any property of any control. Because of this, you can use data binding to perform actions such as:

  • Setting the text to display in a TextBox, Label, Button, LinkButton, or other control.
  • Setting the target page (the HREF attribute) of a link control.
  • Binding a CheckBox control to a Boolean value to directly set the checked state of the control.
  • Setting the graphic of an Image control by binding its ImageUrl property to a database column containing the URL or name of a graphics file.
  • Setting the color, font, or size of controls. This can be useful as a way of implementing user preferences that you store in a database or other data store.

In other words, data binding becomes an automatic way of setting any run-time accessible property of any control on the page.

Binding Multi-Record and Single-Value Controls

There are two types of data binding that a control might support:

  • Multi-record controls can display one or more data records at once, such as the Repeater, DataList, DataGrid Web server controls and the HtmlSelect HTML server control.
  • Single-value controls can display one data value at a time. This includes most Web server and HTML server controls, including the TextBox, Label, HtmlInputText, and HtmlAnchor controls.
    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 might bind its BackColor property to a single data value.

Data binding works differently depending on whether you are binding to multiple records or to a single data value. To display multiple records, you bind a multi-record control to a data source that contains multiple records — an array, a collection, a data table, and so on. The only requirement is that the data source implement the IEnumerable interface. The default behavior is that when data binding occurs, the multi-record control loops through every data item in the data source.

To bind to a single value, on the other hand, you specify what property to bind and what data value it should get its value from.

For more information about how data binding works in each of these types of controls, see Multi-Record and Single-Value Data Binding for Web Forms Pages.

Binding at Design Time vs. at Run Time

If you are binding controls to data in a dataset, you can specify data-binding properties for controls at design time. This includes binding to dataset tables and to data views. For example, you can use design-time tools to specify that a DataGrid control be bound to a table in a dataset. For more information, see Data Binding Multi-Record Web Server Controls.

Similarly, you can bind a TextBox control to a value in a row returned by a data view. Visual Studio includes tools to help you easily specify this kind of design-time binding. For details, see Data Binding Single-Value Web Server Controls at Design Time.

In some situations, however, it is impractical to specify binding properties at design time. Generally, if the data source you want to bind to does not have an instance on your Web Forms page at design time, you need to perform data binding at run time. Some examples where you might do this include:

  • Binding controls to data returned by executing a SQL statement or a stored procedure in a database
  • Binding to objects of any sort that are not instantiated until run time.

At run time, controls raise a DataBinding event that you can use to fetch data values and assign them to control properties.

See Also

Multi-Record and Single-Value Data Binding for Web Forms Pages | Data-Binding Expressions for Web Forms Pages | Data Binding HTML Server Controls | Data Form Wizard