Data Binding

There will come the time in every web application’s lifetime when it needs to display data. That data will be displayed in ASP.NET controls, but first they need to be bound to that control correctly. In this post we will consider the ways to do so.

There are two types of data-binding: Single-Value Binding and Repeated-Value Binding. Consider the former first.

To bind data to an ASP.NET control which supports it, you use the <%# %> delimiters in the property you want to bind to the data. This happens in the .aspx page, not in the code-behind. Between these delimiters, only valid data binding syntax can occur, for example:

protected string GetUserName()
     return User.Identity.Name;
<asp:TextBox runat=”server” Text=’<%#GetUserName()%>’/>

Note that when you use syntax like this, you must call Page.DataBind in order to enforce data binding. Also note that you don’t have to include the data-binding syntax within a server control. You can use simple HTML tags as well, as long as you have valid data-binding syntax.

There are other expressions (namely $ expressions) that you can use in a similar way. The $ expressions allow you to gather various information declaratively from the web.config file. They must stand in a server control’s property, but you don’t need to call DataBind for them. To set a connection string of an SqlDataSource, you’d use:

<asp:SqlDataSource runat=”server” ConnectionString=”<%$ ConnectionStrings:Northwind %>” />

Repeated-Value Binding

You can bind an entire list of information, or a whole table to a control. Let’s review the former. You can bind a list to the following controls:

  • HtmlSelect
  • ListBox
  • DropDownList
  • CheckBoxList
  • RadioButtonList
  • BulletedList

These controls expose the following properties:

  • DataSource: a data object containing the data you’d like to bind.
  • DataSourceID: the ID of the data source object.
  • DataTextField: the name of the value you’d like to see in your list.
  • DataValueField: the value you’d like to assist to the elements of your list.
  • DataTextFormatString: allows you some formatting on the text specified in the DataTextField property (currency for example).

There are controls in ASP.NET called the Rich Data Controls. You can bind to these controls similarly as to the list controls (via DataSource or DataSourceID) but you are able to fine-tune the appearance of the data (and to bind any number of fields). You are also able to use templates within these controls. Inside a template, you’d use the Eval and Bind expressions, for one- or two way bindings respectively. These expressions come handy when you work with template controls. You can use them to bind values (or HTML tags) to fields in your data. An example:

<asp:Label runat=”server” ID=”MyLabel” Text=’<%#Eval(“UserID”)%> />
<asp:TextBox runat=”server” ID=”MyBox” Text=’<%#Bind(“UserName”)%> />

Note that controls bound this way must have an ID.

Further Readings:

Data-Binding Expressions Overview
ASP.NET Data-Bound Server Controls Overview
DataBinder.Eval Method

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s