Skip to main content

Bind to Data

  • 4 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.

Binding to Large Data (Database Server Mode)

The ASPxComboBox, ASPxTokenBox and ASPxListBox controls support database server mode. In this mode, list editors load only required (visible) items to the server memory and implement data-aware operations (for example, filtering) at the database level.

Bind a list editor to a LinqServerModeDataSource or EntityServerModeDataSource and set the editor’s EnableCallbackMode property to true to enable database server mode. When a user applies a filter to a combo box or list box, the editor loads the filtered data to the server memory and displays this data. You can also use the CustomFiltering event to implement a custom filter on the server in this mode.

Tip

Refer to the Members Table for a list of available API.

ASPxGridView_DataBinding_ServerMode

<dx:ASPxComboBox ID="ASPxComboBox1" runat="server" EnableCallbackMode="true" CallbackPageSize="10"
    ValueField="ID" TextFormatString="{0} {1} {2}" Width="287px" DropDownStyle="DropDown" DataSourceID="EntityServerModeDataSource">
    <ClientSideEvents BeginCallback="function(s, e) { OnBeginCallback(); }" EndCallback="function(s, e) { OnEndCallback(); } " />
    <Columns>
        <dx:ListBoxColumn FieldName="FirstName" />
        <dx:ListBoxColumn FieldName="LastName" />
        <dx:ListBoxColumn FieldName="Phone" />
    </Columns>
</dx:ASPxComboBox>
<dx:EntityServerModeDataSource ID="EntityServerModeDataSource" runat="server" 
ContextTypeName="DevExpress.Web.Demos.LargeDatabaseContext" TableName="Persons" />

Note

In database server mode, the ValueType property and the data type of the editor’s value should be of the same type.

Online Demo

Large Database: Server Mode

Members Table

The table bellow lists APIs that DevExpress MVC List Editors use when working with large data sets in database server mode.

Class

API

ASPxComboBox

ASPxAutoCompleteBoxBase.EnableCallbackMode

ASPxAutoCompleteBoxBase.CustomFiltering

ASPxTokenBox

ASPxAutoCompleteBoxBase.EnableCallbackMode

ASPxAutoCompleteBoxBase.CustomFiltering

ASPxListBox

ASPxListBox.EnableCallbackMode

ASPxListBox.CustomFiltering

Database Server Mode Limitations

In database server mode, list editors transform filter expressions (Criteria language syntax) to SQL requests. If list editors (in multi-column mode) use the TextFormatString property, the filter expression ignores the “alignment” and “format string” format parameters.

The following table illustrates how the editor converts its format string in regular data binding mode and database server mode:

Regular Data Binding Database Server Mode
“{0:d} from {1,2:n}” “{0} from {1}”

Handle the Customfiltering server-side event and specify the ListEditCustomFilteringEventArgs.FilterExpression property to use format string functionality to filter list editor items. Refer to the Members Table for a list of available API.