aspxtreme

Data Binding Expression Syntax

ASP.NET Syntax   ASP.NET Page Syntax


Data binding expressions create bindings between any property on an ASP.NET page, including server control properties, and a data source when the DataBind method is called on the page.

You can include data binding expressions on the value side of an attribute/value pair in the opening tag of a server control,

<tagprefix:tagname property = "<%# databinding expression %>" runat="server" />

or anywhere in the page.

literal text <%# databinding expression %>

Attributes


property The control property for which this data binding is declared.
databinding expression Any expression that conforms to the requirements outlined in the remarks section below.

Remarks

All data binding expressions must be contained within <%# and %> delimiters, regardless of where you place them.

ASP.NET supports a hierarchical data binding model that supports associative bindings between server control properties and parent data sources. Any server control property can be data bound against any public field or property on the containing page or on the server control's immediate naming container.

Data-binding expressions use the Eval and Bind methods to bind data to controls and submit changes back to the database. The Eval function is used to define one-way ( read-only ) binding, while the Bind function is used for two-way ( updatable ) binding.

The Eval method 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 controls and submit any changes made back to the database.

Using the Eval Method

The Eval method evaluates late-bound data binding expressions and optionally formats the result as a string.

At run time, Eval invokes the Eval method of the DataBinder class, referencing the current data item of the naming container. The naming container is generally the smallest part of the data-bound control that contains a whole record, such as a row in a GridView control. For this reason, Eval is particularly useful only when data binding controls within templates of a data-bound control.

The Eval method takes the name of a data field and returns a string containing the value of that field from the current record in the data source. You can supply an optional second parameter to specify a format for the returned string, as shown in the following example.

<%# Eval ( "Price", "{0:c}" ) %>

The string format parameter uses the syntax defined for the Format method of the String class.

Using the Bind Method

The Bind method has some similarities to the Eval method, but there are significant differences. Although you can retrieve the values of data-bound fields with the Bind method, as you can with the Eval method, the Bind method is also used when data can be modified.

In ASP.NET, data-bound controls such as the GridView, DetailsView, and FormView controls can automatically use the update, delete, and insert operations of a data source control. For example, if you have defined SQL Select, Insert, Delete, and Update statements for your data source control, using Bind in a GridView, DetailsView, or FormView control template enables the control to extract values from child controls in the template and pass them to the data source control. The data source control in turn performs the appropriate command for the database. For this reason, Bind is particularly useful when data binding controls within an EditItemTemplate or InsertItemTemplate.

The Bind method is typically used with input controls such as the TextBox control rendered by a data-bound row in edit mode. When the data-bound control creates these input controls as part of its own rendering, it can extract the input values.

The Bind method takes the name of a data field to associate with the bound property, as shown in the following example.

<%# Bind ( "fieldName" ) %>

When the row is updated, the values of each control property bound using Bind syntax are extracted and passed to the data source control for the update operation.

See Also

Data Binding in Web Forms



Books and more ...


Suggested Reading

Need a break ?



Previous page Back to top Next page

Check out related books at Amazon

© 2000-2008 Rey Nuñez All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note

You can help support aspxtreme