Binding to Data
- 2 minutes to read
You can bind data editors to data in the same manner as standard server controls. You can use standard data-binding expressions placed between <%# and %>. You can bind almost any editor property against any public field or property on the page or on the editor’s 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 you can bind editors that contain item lists (such as the ASPxComboBox, ASPxListBox, ASPxRadioButtonList) to a data source. The editor retrieves item display settings (such as caption text, associated value, displayed image) from the data source fields specified by editor properties.
Refer to the following demo for editor data binding examples: Editor Data Binding.
<dx:ASPxTextBox ID="tbName" runat="server" Width="100%" Value='<%# Bind("Name") %>' MaxLength="128"> ... </dx:ASPxTextBox> <dx:ASPxRadioButtonList ID="rblGender" runat="server" BackColor="Transparent" Value='<%# Bind("Gender") %>' RepeatDirection="Horizontal" EncodeHtml="False"> <Items> ... </Items> </dx:ASPxRadioButtonList> <dx:ASPxDateEdit ID="deBirthday" runat="server" Value='<%# Bind("Birthday") %>' Width="100%" EditFormatString="MM/dd/yyyy"> ... </dx:ASPxDateEdit>
List Editor Data Binding
When you use binding expressions in a list editor (ASPxComboBox or ASPxListBox) 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 bound at this time and the list editor (or the editor) cannot evaluate data expressions.
Use the DataBindItems (ASPxAutoCompleteBoxBase.DataBindItems/ASPxListBox.DataBindItems) method instead to prevent the exception. The method is similar to the DataBind method, but prevents the base.DataBind method call. As a result, the editor does not evaluate data expressions and does not throw the exception. 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.