Home > Getting Started > ASP.NET Syntax > ASP.NET Page Syntax > Data Binding Expression Syntax
A data binding expression creates a link, or a binding, between a property on an ASP.NET page and a value or expression from a data source.
The symbols <%# and %> are used to delimit or set the bounds of a <%# databinding expression %
>.
A databinding expression can be any valid expression that conforms to the requirements outlined below.
Data binding expressions are typically declared on the value side of an attribute=value pair, in the opening tag of a server control.
<controlname propertyname="<%# databinding expression %>" runat="server" />
Data binding expressions can also be declared anywhere in the page.
literal text <%# databinding expression %>
When binding to a data source, 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 requires the name of a data field, and returns a string containing the value of that field from the current record in the data source.
<%# Eval ( "fieldName" ) %>
Eval can optionally specify a format specifier to render the result into a desired string format.
<%# Eval ( "fieldName", "{0:c}" ) %>
NOTE: Starting in .NET Framework 4.5, the Container.DataItem argument required in earlier versions is no longer used and thus should be ommitted.
Eval is particularly useful when binding to data controls within a templated list, since often both the data row and the data field must be cast.
The below shows using the basic form of Eval.
<itemtemplate>
<table>
<tr align="center" valign="top">
<td valign="top">
<asp:hyperlink runat="server" target="_book"
imageurl='<%# Eval ( "ImageUrl" ) %>'
navigateurl='<%# Eval ( "NavigateUrl" ) %>'
tooltip='<%# Eval ( "AlternateText" ) %>' />
</td></tr>
<tr align="center" valign="top">
<td valign="top">
<asp:hyperlink id="txtLink" runat="server" target="_book"
navigateurl='<%# Eval ( "NavigateUrl" ) %>'
tooltip='<%# Eval ( "Keyword" ) %>' />
</td></tr>
</table>
</itemtemplate>
The below shows using Eval with an optional format specifier.
<itemtemplate>
<table>
<tr>
<td>Date:</td>
<td><b><%# Eval ( "messageDate", "{0:f}" ) %></b></td></tr>
...
</table>
</itemtemplate>
The format parameter uses the syntax defined for the Format method of the String class.
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.
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 are 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.
Like the Eval method, Bind requires the name of a data field, and returns a string containing the value of that field from the current record in the data source.
<%# 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.
Show me
While not commonly used, here we look at an example of using databinding to link, or bind, a property of a control on an ASP.NET page with a value or expression from another control on the page, using the same syntax.
<controlname propertyname="<%# databinding expression %>" runat="server" />
In this case, though, we do not need to use the Eval or Bind methods.
The below code snippet demonstrates this scenario. When a user selects a value from the DropDownList control, the Label control binds against the selected item in the list.
<asp:dropdownlist id="lstStates" runat="server"
datatextfield="statename" />
<asp:button text="Submit" onClick="getState" runat="server" />
<p>Selected State: <asp:label text='<%# lstStates.Text %>' runat="server" />
Rather than explictly pull out the variable from the dropdownlist and then manipulate a label control, here we just call Page.DataBind instead. This evaluates any <%# ... %
> expressions within the page.
Show me
Data Binding in Web Forms