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  : Page 3

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.

Using the Data Binding Framework
To use this data binding framework you create an array of DataBindingRegister instances and populate them with the necessary data binding information in the Page_Load event of the web form. This array is then added to an instance of the DataBindingRegisterCollection class that holds a collection of DataBindingRegister instances:

   void Page_Load(object sender, EventArgs e) {
      DataBindingRegister [] dataBindingRegister = new 
      // You would normally store the following binding information       
      // in an XML file
      dataBindingRegister[0] = new DataBindingRegister();
      dataBindingRegister[0].ControlObject = this.txtEmpName;
      dataBindingRegister[0].ControlObjectPropertyName = "Text";
      dataBindingRegister[0].BusinessObject= "employee";
      dataBindingRegister[0].BusinessObjectPropertyName = "EmpName";
      dataBindingRegister[1] = new DataBindingRegister();
      dataBindingRegister[1].ControlObject = this.txtEmpAddress;
      dataBindingRegister[1].ControlObjectPropertyName = "Text";
      dataBindingRegister[1].BusinessObject = "employee";
      dataBindingRegister[1].BusinessObjectPropertyName = 
      //Now, add the data binding information to the 
      // Data Binding Collection Register
      if (!IsPostBack)
         employeeDO = ReadData();
         this.bindingManager.BusinessEntity = employeeDO;
         this.bindingManager.Direction = 
After storing the data binding information you can bind data from the controls in the user's interface to the corresponding properties of the business object and vice-versa using the BindingManager.Bind() method. This method accepts an instance of the Page class as a parameter, iterates through the collection of databound control instances and then binds or un-binds data—depending on the binding direction set earlier using the BindingDirection enumeration. The Bind() method returns an IBusinessEntity instance.

   IBusinessEntity IBindingManager.Bind(Page form)
      foreach (DataBindingRegister dbR in 
         Control control = dbR.ControlObject;
         if (control.ID != null) {
            if (this.Direction == 
            else if (bindingDirection == 
               PropertyInfo propertyInfo = control.GetType().
                  object obj = propertyInfo.GetValue(control, null);
                  propertyInfo = this.BusinessEntity.GetType().
                     this.BusinessEntity, obj, null);
      return this.BusinessEntity;
Figure 1. Sample Application: Here's the sample application showing the two-way databound controls.
The preceding example calls the Bind() method twice: once in the Page_Load event and once in the Button_Click event. The BindingDirection enum will hold one of two values: either FromBusinessObjectToUI or FromUIToBusinessObject.

That completes the application. Upon execution, it displays the default data bound to the controls in the Page_Load event (see Figure 1). And if you enter new values in the text boxes and then click on the Button control, the form displays the name and address entered in the text boxes after a postback. In both cases, the data binding takes place using the Bind() method, which executes once in each direction.

Data binding in ASP.NET 1.x was unidirectional, i.e., the control could in no way update the underlying data source to which it was bound. While ASP.NET 2.0 introduced two-way data binding, not all the ASP.NET controls supported i; you need to write custom data binding logic such as that discussed in this article to make two-way data binding available for all the controls.

Joydip Kanjilal has over 10 years of industry experience with C, C++, Java, C#, VB, VC++, ASP.Net, XML, Design Patterns, UML, etc. He currently works as a senior project leader in a reputable multinational company in Hyderabad, India, and has contributed articles on .NET and related technologies to www.aspalliance.com.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date