Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

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.


advertisement
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 DataBindingRegister[2]; // 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 = "EmpAddress"; //Now, add the data binding information to the // Data Binding Collection Register this.bindingManager.DataBindings.Add(dataBindingRegister); if (!IsPostBack) { employeeDO = ReadData(); this.bindingManager.BusinessEntity = employeeDO; this.bindingManager.Direction = BindingDirection.FromUIToBusinessObject; this.bindingManager.Bind(this); } }

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 this.dataBindingCollection) { Control control = dbR.ControlObject; if (control.ID != null) { if (this.Direction == BindingDirection.FromUIToBusinessObject) { control.DataBind(); } else if (bindingDirection == BindingDirection.FromBusinessObjectToUI) { PropertyInfo propertyInfo = control.GetType(). GetProperty(dbR.ControlObjectPropertyName, BINDING_FLAGS); object obj = propertyInfo.GetValue(control, null); propertyInfo = this.BusinessEntity.GetType(). GetProperty(dbR.BusinessObjectPropertyName, BINDING_FLAGS); propertyInfo.SetValue( 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.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap