Binding to Data
- 2 minutes to read
Data editors can be bound to data in the same manner as standard server controls. You can use standard data-binding expressions placed between <%# and %>. Almost any editor property can be bound against any public field or property on the page or on the editor’s immediate naming container.
In data-binding expressions, use the Eval and Bind methods to bind data to editors and submit changes back to the database. The Eval method is a static (read-only) method that takes the value of a data field and returns it as a string. The Bind method supports read/write functionality with the ability to retrieve the values of data-bound editors and submit any changes made back to the database.
Note that editors containing item lists (such as the ASPxComboBox, ASPxListBox, ASPxRadioButtonList) can be bound to a data source and fetch list items dynamically. Item display settings (such as caption text, associated value, displayed image) are retrieved from the data source’s fields specified via editor properties.
Refer to our Editor Data Binding demo for editor data binding examples.
List Editors Data Binding
When you use binding expressions in a list editor (ASPxComboBox or ASPxListBox), which is placed within a bound control, then the editor’s DataBind (ASPxComboBox.DataBind/ASPxWebControl.DataBind) method can cause the following exception: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control. The exception occurs because the external control is not yet bound by this time and data expressions cannot be evaluated.
To prevent the exception, use the DataBindItems (ASPxAutoCompleteBoxBase.DataBindItems/ASPxListBox.DataBindItems) method instead. The method is similar to the DataBind method, but prevents the base.DataBind method call. As the result the data expressions are not evaluated and the exception is not thrown. Note that the DataBindingItems method does not raise the DataBinding event.
The problem described above can also appear when you bind a list editor within its Callback event handler (ASPxAutoCompleteBoxBase.Callback/ASPxListBox.Callback). To prevent the exception, a list editor ignores the DataBind method call within a Callback event handler, and calls the DataBindItems method instead. As a result, the DataBinding event is not raised.