Data-Binding Expressions for Web Forms Pages
The document is archived and information here might be outdated

Data-Binding Expressions for Web Forms Pages

Visual Studio .NET 2003

Data binding individual control properties in Web Forms pages is not implemented by directly tying the property to a source of data. Instead, data binding is implemented by using a special expression format. Information about what data you want to bind to is built into the expression; you then assign the results of the expression to a control property.

For example, imagine that you want to bind a TextBox Web server control to some data. 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 example shows what the control declaration might look like in HTML view. The control's Text property is bound to a data view that contains a single record. The data-binding expression is delimited with the characters <%# and %>.

<asp:TextBox id="TextBox1" runat="server" 
   Text='<%# DataView1(0)("au_lname") %>'>

Similarly, you can use a data-binding expression to set the ImageUrl property of an Image Web server control. In that case, you are extracting a string from the database that contains the path and filename of the graphic to display. An example might look like the following:

<asp:Image id=Image1 runat="server" 
   ImageUrl='<%# DataView(0)("productPhotoURL") %>'>

In Visual Studio, the Properties window provides you with a tool to create data-binding expressions. Alternatively, you can create them yourself and enter them in HTML view of the Web Forms designer.

Advantages of Using Data-binding Expressions

Using an expression for data binding gives you flexibility in a number of ways:

  • You can use any expression, as long as it resolves to a value or values the control can use. Most frequently, the data-binding expression will resolve to a value derived from a data source, but it can also reference properties of the page or of other controls, values that you calculate at run time, or almost anything else.
  • You can assign the expression to any property — that is, you can bind any property to data. For example, you can keep information about user preferences in a database and use data binding to implement those preferences in properties for fonts, colors, sizes, styles, and so on. In addition, you can bind more than one control property, which allows you to bind one property to one data source, and another property to a different source.

Using the DataBinder Class for Binding

Although 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. This is most often a table in a dataset or a data view that contains the single record you are interested in. To simplify this type of data binding, ASP.NET server controls support a class called DataBinder that performs some of the work of extracting data and making it available to a control property.

You use the DataBinder class by invoking its Eval method, which requires two parameters:

  • A reference to the data container, typically the dataset, data table, or data view.
  • A reference to the individual value to be derived. This frequently references a single row (row zero) and a column value in that row.

The following example shows the same data binding that was done above for a text box, but this time using the DataBinder class.

<asp:TextBox id="TextBox1" runat="server" 
   Text='<%# DataBinder.Eval(DataView1, "[0].au_lname") %>'>

The earlier example of setting the ImageUrl property of an Image control might look like the following. In this example, a formatting expression is passed in the optional second parameter of the DataBinder.Eval method; the expression adds a path onto the data as a prefix.

<asp:Image id=Image1 runat="server" 
   ImageUrl='<%# DataBinder.Eval(Container, "DataItem.ProductImage", "http://myserver/myapps/images/{0}") %>'>

The advantages of using the DataBinder class are:

  • The syntax is consistent for all binding, enforced by the parameters required for the Eval method.
  • The DataBinder class is supported by the Visual Studio design tools for Web Forms pages.
  • The class performs type conversion automatically. For example, if you are binding a text box to a data column containing an integer, the DataBinder class will convert the integer to a string automatically.
  • You can optionally specify a formatting expression that can convert or amend the data.

Resolving Data-binding Expressions

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:

' Visual Basic
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   SqlDataAdapter1.Fill(DsAuthors1, "authors")
   If Not (Me.IsPostBack) Then
   End If
End Sub

// C#
private void Page_Load(object sender, System.EventArgs e)
   SqlDataAdapter1.Fill(dsAuthors1, "authors");
   if (!this.IsPostBack) 

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.

See Also

Data Binding Expression Syntax | Multi-Record and Single-Value Data Binding for Web Forms Pages | Data Binding Single-Value Web Server Controls at Design Time | Data Binding HTML Server Controls

© 2016 Microsoft