Home > ASP.NET Web Forms > Data Binding in Web Forms > Data Binding Expressions for Web Forms Pages
Data binding in Web Forms is implemented by using a special expression format.
The symbols <%# and %> are used to delimit or set the bounds of a <%# databinding expression %
>.
Information about what you want to bind to is built into the expression, and you then assign the results of the expression to a control property.
For example, you need to bind a Label control to some data on the page. You create a data-binding expression and assign it to the control’s Text property so that the value will be displayed in the control.
The following shows what the control declaration might look like in HTML. In this example, the Label’s Text property is bound to the selected item of a DropDownList control.
<p>Selected State: <asp:label text='<%# lstStates.Text %>' runat="server" />
While you can data bind using virtually any expression that resolves to a value, in most cases you will bind to a data source of some sort, which is most often a value from a table in a dataset or a data view.
To simplify this type of data binding, ASP.NET includes the DataBinder class that performs most of the work of extracting data and making it available to a control property.
When binding to a data source, you simply invoke the DataBinder.Eval method to bind data to controls.
The Eval method requires a reference to where the value is to be derived, which is the name of a data field. At runtime, Eval returns a string containing the value of that field in the data source.
<%# Eval ( "fieldName" ) %>
You can optionally specify a format specifier as a second argument, 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.
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.
Data-binding expressions must be resolved at run time in order to provide values that controls can bind to.
You explicitly perform this step during page processing by calling the DataBind method, which is a method of the System.Web.UI.Control class.
You can call the method for individual controls or, more efficiently, you can call it for the Page class ( which also derives from the Control class ). The method cascades the call to all child controls, so by calling it once for the page, you invoke it for all controls on the page.
You typically call the DataBind method under these circumstances:
- The first time the page runs, but after the data source has been populated ( for example, after you have filled a dataset ).
- After the data source has changed ( for example, because you have updated records in it ).
The following example shows a typical way to call the DataBind method during the page initialization event:
private void Page_Load ( object src, EventArgs e ) {
myAdapter.Fill ( DsAuthors1, "authors" );
if ( !this.IsPostBack ) {
this.DataBind ( );
}
}
Private Sub Page_Load ( ByVal src As Object, ByVal e As EventArgs )
myAdapter.Fill ( DsAuthors1, "authors" )
If Not ( Me.IsPostBack ) Then
Me.DataBind ( )
End If
End Sub |
|
C# |
VB |
You generally do not want to call the DataBind method on each round trip ( that is, in the page initialization without checking for a post back ), because doing so replaces the values in controls.
For example, if you are working with a DataGrid control, the control might contain changes that you want to process. When you call DataBind, the contents of the grid are replaced with values from the data source.
If you do this during page initialization, you will lose the changes in the grid before you get a chance to process them. Instead, you usually call the DataBind method in an event handler, after you have performed the data processing for that event.