aspxtreme

Introduction to Data Binding in Web Forms

ASP.NET Web Forms   Data Binding in Web Forms


Web Forms enable the display of dynamic content by providing a simple yet flexible means of binding controls to a data source.

Data binding in Web Forms, though, works somewhat differently than it does in traditional forms-based applcations, such as Windows desktop applications. This is mainly due to the nature of HTTP, a Web standard that governs how a Web server responds to a Web browser request in general.

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 Overview

The data-binding architecture of Web Forms pages is based on the presumption that data access in the vast majority of Web applications is read-only — that there is much more often need to display information from a data store, than there is to write data back to the store.

Typical examples of applications that present read-only data are

  • pages that allow users to choose from various types of information, such as articles and news on, say, current events, technology, entertainment and so on.
  • a search results page for related Web pages, which 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.

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.

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.

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.



Books and more ...


Suggested Reading

Need a break ?



Previous page Back to top Next page

Check out related books at Amazon

© 2000-2008 Rey Nuñez All rights reserved.

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

You can help support aspxtreme