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 2

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.

Sample Two-Way Data Binding Application
To test the framework, you need to build an application that uses it. As a simple example, suppose you have an Employee entity, represented by an EmployeeDO class. This class contains three private fields and corresponding exposed public properties. It implements the IBusinessEntity interface and extends the Component class. IBusinessEntity is a base interface that all entities in this application extend, but this sample application deals exclusively with the Employee entity.

Here's the code for the IBusinessEntity interface:

   public interface IBusinessEntity
      Object Code
The IBusinessEntity interface exposes one property called Code, which functions as a unique ID. Using the IBusinessEntity interface as the base interface or contract for all entities ensures that you can reuse the data binding framework code with any business object. Here's the code for the EmployeeDO class:

   public class EmployeeDO : Component, IBusinessEntity
      private object code;
      private string empName;
      private string empAddress;
      public Object Code
         get {return code; }
         set {code = value; }
      public String EmpName
         get {return empName; }
         set {empName = value; }
      public String EmpAddress
         get {return empAddress; }
         set {empAddress = value; }
Create a Container
Next, you need a container in which to store the data binding information. You'll use this information to bind the controls to the appropriate property of the business object at runtime. This DataBindingRegister class serves that purpose. It holds the data binding information for each control along with the corresponding property of the business object to which the control will be bound.

Here's the source code for the DataBindingRegister class:

   public class DataBindingRegister
      #region Fields
      protected string businessObj;
      protected string objPropertyName;
      protected Control ctrlObj;
      protected string ctrlPropertyName;
      #region Properties
      public string BusinessObject {
         get { return businessObj; }
         set { businessObj = value; }
      public string BusinessObjectPropertyName
         get { return objPropertyName; }
         set { objPropertyName = value; }

To use the data binding framework you create an array of DataBindingRegister instances and populate them with the necessary data binding information when the web form loads.
public Control ControlObject { get { return ctrlObj; } set { ctrlObj = value; } } public string ControlObjectPropertyName { get { return ctrlPropertyName; } set { ctrlPropertyName = value; } } #endregion }
Build the UI
Finally, you need to build the user interface, which contains a Label that displays the employee's code, two TextBoxes that display the employee's name and address respectively, and a Button control.

The following code snippet shows the controls used in the .aspx file for the sample application:

   <asp:label id=lblEmpCode  runat="server" 
      Text='<%# DataBinder.Eval(employeeDO,"Code") %>'>
   <asp:textbox id=txtEmpName runat="server" 
      Text='<%# DataBinder.Eval(employeeDO, "EmpName") %>'>
   <asp:textbox id=txtEmpAddress runat="server" 
      Text='<%# DataBinder.Eval(employeeDO, "EmpAddress") %>'>
   <asp:Button ID="Button1" runat="server" 
      OnClick="Button1_Click" Text="Button" />

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