RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Take Advantage of Two-Way Data Binding in ASP.NET

It's always been easy to display data in ASP.NET by binding it to controls. But now, ASP.NET 2.0 lets you bind data that's changed or entered in the controls back to the data source as well.

ata binding is an indispensable feature of ASP.NET that links a user interface element with the underlying data store. ASP.NET 1.1 restricted this data binding to one direction only—from the business object to the web form; but ASP.NET 2.0 introduces support for binding data in both directions. This more flexible approach reduces code, simplifies data binding logic, and simplifies maintenance.

This article explores two-way data binding and illustrates how you can use the feature. The sample application that accompanies the article walks you through the process for implementing two-way data binding in ASP.NET.

What is Data Binding?
The term "data binding" refers to the act of associating a control to an underlying data source in such a way that changes to the data source initiate similar changes in the data contained in the control. Note that controls are bound to an "intermediate data source" and not the "original data source;" in other words the data controls are not bound to data over an active connection, but to an in-memory representation of data.

Understanding Two-Way Data Binding
One familiar example of two-way data binding is the DataSet/DataAdapter combination. You can update the data in the DataSet, and then update the underlying data source using the DataAdapter, or, if changes occur to the underlying data, you can refresh the DataSet using the Fill() method.

With ASP.NET 2.0's bi-directional data binding, you can eliminate the code required to update a data store by binding the data controls in your presentation layer to the data sources. Because the binding is bi-directional, when users modify the data in the controls the underlying data sources update when users post the web form back to the server.

Typically, you've had to use code similar to the following to manage displayed data binding:

   //Populate the data controls from a business object
   txtCode.Text = emp.Code;
   txtName.Text = emp.Name;
   //Populate a business object from the data controls
   emp.Code = txtCode.Text;
   emp.Name = txtName.Text;
To enable two-way binding you use the Bind() method.
If that looks familiar, I'm sure you're aware of what happens when you have many controls that you need to bind to data—the code volume increases dramatically.

Here's the simpler approach in ASP.NET 2.0. To enable two-way binding you use the Bind() method as in the following example:

   <%# Bind("FieldName") %>
Note that you embed data binding expressions in ASP.NET within special <%# and %> tags. You can use the related Eval() method for one-way read-only data binding, but you must use Bind() for two-way data binding where you might need to update data.

Unfortunately, not all ASP.NET controls support two-way data binding. To work around that, the downloadable sample application that accompanies this article shows you how to implement a two way data binding framework that you can use to bind data to any ASP.NET control.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date