The DataBinder class provides two-way, simple databinding a single datasource value and single control property. It can bind object properties and fields and database values (DataRow fields) to a control property such as the Text, Checked or SelectedValue properties. In a nutshell the controls acts as a connector between a datasource and the control and provides explicit databinding for the control.
The control supports two-way binding. Control can be bound to the datasource values and can be unbound by taking control values and storing them back into the datasource. The process is performed explicitly by calling the DataBind() and Unbind() methods of the control. Controls attached to the databinder can also be bound individually.
The control also provides a BindErrors collection which captures any binding errors and allows friendly display of these binding errors using the ToHtml() method. BindingErrors can be manually added and so application level errors can be handled the same way as binding errors. It's also possible to pull in ASP.NET Validator control errors.
Simple validation is available with IsRequired for each DataBinding item. Additional validation can be performed server side by implementing the ValidateControl event which allows you to write application level validation code.
This control is implemented as an Extender control that extends any Control based class. This means you can databind to ANY control class and its properties with this component.
System.Web.UI.Control
Westwind.Web.Controls.DataBinder
public class DataBinder : Control, IExtenderProvider
Class Members
Member | Description | |
---|---|---|
Constructor |
||
AfterBindControl |
Fires immediately after the control has been bound. You can check for errors or perform additional validation. | |
AfterUnbindControl |
Fires immediately after binding is complete. You can check for errors and take additional action. | |
BeforeBindControl |
Fired just before the control is bound. You can return false from the handler to cause the control to not be bound | |
BeforeUnbindControl |
Fires immediately before unbinding of a control takes place. You can return false to abort DataUnbinding. | |
ValidateControl |
Event that can be hooked to validate each control after it's been unbound. Allows for doing application level validation of the data once it's been returned. | |
DesignMode |
||
AddBinding |
Adds a binding to the control. This method is a simple way to establish a binding. public DataBindingItem AddBinding(Control ControlToBind, string ControlPropertyToBind, object SourceObjectToBindTo, string SourceMemberToBindTo) public DataBindingItem AddBinding(Control ControlToBind, string ControlPropertyToBind, string SourceObjectNameToBindTo, string SourceMemberToBindTo) public DataBindingItem AddBinding(Control ControlToBind) |
|
AddBindingError |
Adds a binding error message to a specific control attached to this binder BindingErrors collection. public bool AddBindingError(string ErrorMessage, string ControlName) public bool AddBindingError(string ErrorMessage, Control Control) public bool AddBindingError(string ErrorMessage, DataBindingItem BindingItem) |
|
AddValidationErrorsToBindingErrors |
Takes a collection of ValidationErrors and assigns it to the matching controls. The IList object should have Message and ControlID (optional) properties. public void AddValidationErrorsToBindingErrors(IList errors) |
|
CanExtend |
Determines whether a control can be extended. Basically we allow ANYTHING to be extended so all controls except the databinder itself are extendable. public sealed bool CanExtend(object extendee) |
|
DataBind |
Performs data binding against each of the defined DataBindingItems defined on the DataBinder control. This binds all BindingSources to the specified control properties. public bool DataBind() public bool DataBind(Control Container) |
|
GetDataBindingItem |
Returns a specific DataBinding Item for a given control. Always returns an item even if the Control is not found. If you need to check whether this is a valid item check the BindingSource property for being blank. public DataBindingItem GetDataBindingItem(Control control) public DataBindingItem GetDataBindingItem(string ControlId) |
|
LoadFromControls |
Picks up all controls on the form that implement the IDataBinder interface and adds them to the DataBindingItems Collection public void LoadFromControls(Control Container) |
|
OnBeforeUnbindControl |
Fires the BeforeUnbindControl event public bool OnBeforeUnbindControl(DataBindingItem Item) |
|
OnValidateControl |
Fires the ValidateControlEvent public bool OnValidateControl(DataBindingItem Item) |
|
Unbind |
Unbinds value from controls back into their underlying binding sources for the defined DataBinding items of this control. Returns true on success false on failure. public bool Unbind() public bool Unbind(Control Container) |
|
AutoLoadDataBoundControls |
Automatically imports all controls on the form that implement the IDataBinder interface and adds them to the DataBinder | |
BindingErrorMessage |
Binding Error message when a control fails to bind | |
BindingErrors |
A collection of binding errors that is filled after binding or unbinding if errors occur during binding and unbinding. | |
DataBindingItems |
A collection of all the DataBindingItems that are to be bound. Each <DataBindingItem> contains the information needed to bind and unbind a DataSource to a Control property. | |
DefaultBindingSource |
A default binding source that is used if the binding source on an individual item is not set. | |
ErrorIconUrl |
Optional Url to the Warning and Info Icons. Note: Used only if the control uses images. | |
HideValidators |
Hides the Validator display and uses only the DataBinder's display for errors | |
IsExtender |
Determines whether this control works as an Extender object to other controls on the form. In some situations it might be useful to disable the extender functionality such as when all databinding is driven through code or when using the IDataBinder interface with custom designed controls that have their own DataBinder objects. | |
IsRequiredErrorMessage |
Message displayed when IsRequired is blank. | |
ShowBindingErrorsOnControls |
Determines whether binding errors are display on controls. | |
UnBindingErrorMessage |
Error displayed when an unbinding error occurs. Typically this will be some sort of format conversion problem {0} denotes the derived field name. | |
UseClientScriptHtmlInjection |
Determines whether the control uses client script to inject error notification icons/messages into the page. Setting this flag to true causes JavaScript to be added to the page to create the messages. If false, the DataBinder uses Controls.Add to add controls to the Page or other Containers. |
Requirements
Namespace: Westwind.Web.ControlsAssembly: westwind.web.webforms.dll
See also:
Databinding with DataBinder© West Wind Technologies, 1996-2016 • Updated: 12/12/15
Comment or report problem with topic