Published: 24 Apr 2009
By: Iftekharul Anam

This article will give you a highlight on implementing “Live Binding”, a feature for the upcoming version of ASP.NET AJAX.

Contents [hide]

Introduction

Many of you may already be familiar with the upcoming version of ASP.NET AJAX, version 4.0. The release was out on March 12, 2009. The key features for this version are:

  • ADO.NET Data Services support
  • WCF and ASMX Web service integration
  • ASP.NET AJAX Client Templates
  • Declarative instantiation of client-side controls and behaviors
  • Observable pattern for plain JavaScript objects
  • Live Bindings
  • Markup Extensions
  • DataView control and DataContext component
  • Command Bubbling
  • Change tracking and identity management
  • Support for managing complex links and associations between entities from multiple entity sets or tables
  • Extension methods allowing change tracking and read-write client-server scenarios using other JSON services, including RESTful or JSONP based services

This preview version can be downloaded from here. This writing is mostly for highlighting the "Live Binding" feature introduced in this release of ASP.NET AJAX. This will also cover some operations on DataView control and DataContext component.

What is Live Binding?

Live binding is having the data bound in real-time. Meaning when there's any change in the data source, the changes are reflected to the data bound interface instantly and vice versa. For example if you have an interface component, like a table, bound to a data source, like an array, any change to that array from the code is reflected in the view table instantly. If you are using an edit template for updating the data of a selected row of the table, the data in the table will get updated as you change a field in your edit form if both the table and the form use "Live Binding". Pretty cool, right?

Inside Live Binding

This is all done through client-side script, JavaScript. But how does it come in action? The core of the live binding is the implementation of an "Observer" pattern. The observer pattern enables an object to be notified about changes that occur in another object. This is not an event handler based pattern which we often misuse as an observer pattern. ASP.NET AJAX 4.0 implements this pattern completely. It adds observer functionality to ordinary JavaScript objects or arrays so that they raise change notifications when they are modified through the Sys.Observer interface.

Live Binding in Action

To implement live binding, you will need the ASP.NET AJAX 4.0 framework included in your file. After downloading from here, you'll need to reference them to your file. You can use a conventional <script> tag to reference these files:

Or you can use ScriptReferences under a ScriptManager tag:

You can see from these references that there are 3 files in this package, an updated MicrosoftAjax.js file and 2 new files: MicrosoftAjaxTemplates.js (for the client template support) and MicrosoftAjaxAdoNet.js (for ADO.NET utilities support).

The key components used for the Live Binding are Templates, the DataView control and the DataContext class. The AdoNetDataContext class is also there for additional ADO.NET support.

DataView Control and DataContext class

This control can bind to any JavaScript object, array or even ASP.NET AJAX component. To use a DataView control in your page, the following declarative initialization process is needed:

The value of the "activate" attributes are the comma separated IDs for the HTML components in which the observer is applied to check for changes. Using an * here implies to activate all the HTML components to be observable. But this might get the page rendering speed to be slower.

Data can be provided to a DataView control for live binding in a number of ways.

1. Setting the data property of the control decoratively: A declarative binding can be done through assigning a value to the data property as follows:

2. Setting the data property of the control through code: This approach can be implemented as follows:

3. Using WCF or ASP.NET web service as dataProvider: A WCF or ASP.NET web service can be specified in the dataProvider property of the control.

When the DataView control’s dataProvider property is set, the DataView control will use the provider (in this case, the Web service) to fetch data by using the operation specified in the fetchOperation property.

4. Using DataContext classes as dataProvider: The DataContext class can be used as follows:

If you are using an ADO.NET data service, you should use the AdoNetDataContext class instead of the more general-purpose DataContext class.

One-way/One-time Binding

In the above examples you see a binding syntax like this: { { expression } } This is a one-way/one-time data binding as the expression is evaluated only once, at the time of template rendering. With one-way/one-time binding, if the source data changes after the template has been rendered, the rendered value will not be automatically updated. An example for this is:

Two-way Live Binding

Another syntax is available to ensure the target value is updated with the change of source value, which is like this: { expression } In two-way live binding, the binding works in both directions. If the target value is changed (in this case, the value in the UI), the source value is automatically updated (in this case, the underlying data item). Similarly, if the source value is changed (in this case, if the underlying data value is updated externally), the target value (the value in the UI) is updated in response. As a result, target and source are always in sync.

In the following example, if the user modifies the values in the text boxes, the values in the h3 and div elements will change automatically.

The live-binding syntax is similar to binding syntax in WPF (XAML). It can be used for binding between UI and data, as in the above examples, as well as directly between UI elements, between data and properties of declaratively attached controls and components, and so on.

Additional Features

The syntax also provides functions for converting data values to rendered values, or converting back from values entered in UI to an appropriately formatted data value. The following example shows how to provide conversion functions:

This similar syntax can be used to specify binding mode as well.

The default binding behavior for text-boxes and other input controls is two-way and for all other controls is one-way.

Summary

With the live binding through templates, it will ease the client-side development lot smoother for handling large amount of data. With these new features coming in the ASP.NET AJAX 4.0, life will become more fun in development. Let’s wait and see what more is coming in ASP.NET 4.0.

About Iftekharul Anam

I'm a web professional with long time industry experience with top IT companies like Pageflakes Inc (www.pageflakes.com), Obout Software Inc (www.obout.com), IMS Health (www.imshealth.com). My experience includes various technologies including Asp.Net 1.1/2.0/3.5, AJAX, CSS, JavaScript, Cross-B...

View complete profile here.

Other articles in this category


ASP.NET Ajax Grid and Pager
Learn how to create a custom Ajax Grid and Pager with the Microsoft ASP.NET AJAX platform.
JSON-Enabled WCF Services in ASP.NET 3.5
Dino Esposito overviews the integration between WCF and AJAX
Using jQuery with ASP .NET
A brief introduction to jQuery and ways in which we can integrate it into ASP .NET
ASP.NET AJAX Control Development
An in depth guide to developing controls with the ASP.NET AJAX
ASP.NET Ajax Web Service
Sharing some of the Common Issues of ASP.NET Ajax Web Services.

You might also be interested in the following related blog posts


Announcing Microsoft Ajax Library (Preview 6) and the Microsoft Ajax Minifier read more
Gaia Ajax 3.6 Alpha released: Ajax GridView and Adaptive Rendering ++ read more
How To Cache Rows In DevExpress ASP.NET GridView read more
5 Minute Overview of MVVM in Silverlight read more
Building a class browser with Microsoft Ajax 4.0 Preview 5 read more
Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Part 25: ViewModel read more
ASP.NET AJAX 4.0 Preview 5 released to CodePlex read more
ASP.NET 4 Beta 2 - New Version, New Docs, New MSDN Site ! read more
DevReach Follow-up, Part I read more
Convert Web.UI ASP.NET Grid into a data entry spreadsheet read more
Top
 
 
 

Discussion


Subject Author Date
placeholder Down Loadable Sample Cisco Kid 6/23/2009 10:34 AM
Example Iftekharul Anam 6/23/2009 11:37 AM

Please login to rate or to leave a comment.

Product Spotlight