ASP.Net provides basic databinding for simple controls, but it's fairly limited. ASP.NET 2.0 adds very limited two-databinding by way of the FormsView and DetailsView controls, but it's entirely limited to these controls. Two-way databinding - that is the abillity to bind a control and then also be able to unbind the control values back into its data source - is not supported otherwise.
To provide a better databinding experience we provide a custom DataBinder control whose job it is to provide simple databinding to bind data from plain .NET object properties/fields and DataSets/Tables/Rows to control properties. In a nutshell the databinder allows you to bind anything that can be referenced on the page to a control property. For example, you can bind a DataRow field value to a Textbox.Text property, or you can bind an Customer entity property to a Checkbox Checked property. In essence you can bind any simple property of an object to an ASP.NET control property.
The DataBinder control only provides Simple Databinding, which means each DataBindingItem binds a single value to a single control property. It is a one to one mapping from control to datasource item. It does not do anything for list binding like GridView, ListBox, ListView, Repeater etc. For binding list based data you still use ASP.NET's great support for list binding. Some controls like a ListBox might use both: Simple DataBinding to bind the SelectedValue, but standard ASP.NET list binding to bind the actual items with DataSource/DataValueField/DataTextField.
The DataBinder control provides:
- Two way Databinding
This control supports binding and unbinding of data from a DataSource to Control property and back. Unlike ASP.NET which bind automatically and without any development control, the DataBinder binds deterministically, so you explicitly call DataBinder.DataBind() and DataBinder.Unbind() from code. DataBinding binds a source value to a control property and unbinding stores control value back into the underlying datasource, including checking for formatting errors. A key feature of the unbinding is that it automatically handles type coercion back into the BindingSource's type.
- Bind to anything
The DataBinder can bind to anything that can be referenced through a form instance. You can bind to DataSets/Tables/Rows and fields as well as objects and properties or fields. Each DataBindingItem has a BindingSource ("Customer.DataRow" or Customer.Entity for example) which is the base object to bind to. It also has BindingSourceMember property ("LastName") that identifies the source property or row to bind on the BindingSource object, and a BindingSourceProperty ("Text" or "Checked" or "SelectedValue" - can be any property) that identifies the property to bind on the control. In other words you can bind anything to anything.
- Extender Interface
The DataBinder is implemented as an Extender control which extends all Web Controls on an ASP.NET Page in the Designer. So you can bind to a text box as well as any custom controls and third party controls. The Extender interface exposes a DataBindingItem object on each control which can be set interactively in the Property Sheet properties for the specified control. In the Html markup the DataBinder control holds the actual binding mappings as a collection of DataBindingItems.
- Doesn't interfere with ASP.Net databinding
DataBinder works in conjunction with stock ASP.NET databinding. You can still use list based binding and you can even use standard DataBind() on a Page in addition to the DataBinder control binding.
- Rich Control validation and Error handling
The DataBinder handles binding errors by capturing any binding and unbinding errors into a BindingErrors collection. The collection provides easy access to any errors and these errors can be easily displayed in a rich fashion with the ToHtml() method. Errors link back to the controls in error and allow jumping directly to these controls. The BindingErrors collection can also be manually added to, so you can optionally add business rule validation errors to the same visual error display. DataBinder can also pick up errors from stock ASP.NET Validators optionally.
- Validation Events
The control includes a IsRequired property which when set checks for blank values and flags them automatically as binding errors. The control also includes a ValidateControl event which can be used to validate values during unbinding in event code. This makes it real easy to create validation code and hook it directly into the validation chain when unbinding data.
- Basic formatting support
Controls can display data using standard IFormatProvider formats which is set as a simple property. Unbinding automatically checks for the type of data that is bound back to and - if possible - recognizes standard formats (ie. currency formats, various different kinds of date formatting).
- Deterministic Databinding
The DataBinder control is deterministic which means you and your code controls when data binding and unbinding takes place. Unlike ASP.NET's binding which occurs automatically and is confusing, the DataBinder.DataBind() and DataBinder.Unbind() methods are used explicitly in your code so you can unbind controls under your full control. Binding/Unbinding can be done by container (ie. this.DataBinder.DataBind()) or by control (this.DataBinder.GetDataBindingItem("txtName").DataBind()) which offers you full control over the binding process.
Comment or report problem with topic