Microsoft.NET

……………………………………………….Expertise in .NET Technologies

Data Binding in ASP.NET

Posted by Ravi Varma Thumati on July 21, 2009

Introduction

In ASP.NET, the term Data Binding implies binding the controls to data that is retrieved from the data source and hence providing a read or write connectivity between these controls and the data, that they are bound to. These data sources can be one of databases, xml files or even, flat files.

In ASP.NET 1.x, you were introduced to a powerful data binding technique where you could eliminate the need of writing lengthy code that was used in earlier for binding data to data controls. With ASP.NET 2.0, you have a lot of new controls and features added in this context. You now have simplified paging, filtering, sorting, automatic updates, data source controls, and a host of other powerful features.

The ASP.NET Data Binding Model

In data binding, as we have discussed in the beginning, the controls are bound to data from the data source resulting in read or write connectivity between the controls and the data they are bound to. The controls are actually bound to the columns of the result set that contains the data. This result set can be a data set, a data table, a data reader, or any other instance of a collection type.

In its simplest form, the syntax for using data binding in your ASPX pages is as follows:

<%# Data Source Name %>

Depending on whether you require binding single value data or a multiple or repeated value data to a control, you can have the following types of binding mechanisms in ASP.NET data controls:

  • Single Value Data Binding
  • Repeated Value Data Binding

a single record, say, an employee’s record. In contrast, repeated value data binding implies binding a set or a table of employee records.

You can use any of the following for single value data binding:

<%# Name of the Property %>

<%# Expression %>

<%# Method Name, Parameter List %>

For repeated value data binding, you can use the following syntax:

<%# Name of the Data Source %>

Using the Data Binding Expressions

What are data binding expressions? Well, they are the code snippets that you use between the <%# and %> blocks in your ASP.NET web page.

“Data-binding expressions create bindings between any property on an ASP.NET page, including a server control property, 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 or value pair in the opening tag of a server control or anywhere in the page”.

The following are the advantages of using Data Binding expressions in ASP.NET controls in the presentation layer:

  • Flexibility to use any data binding expressions provided that the value it resolves to is one that the data control can use.
  • You can use these expressions to bind any property to its corresponding data.
  • Flexibility to bind one property to one data source and another property to another data source.

You should use data binding in the ASP.NET web pages in the presentation layer of your application. The syntax used for data binding in ASP.NET 1.x is as follows:

<%# Container.DataItem(“expression”) %>

The following code snippet illustrates how you can bind data to a label control using the syntax shown above:

<asp:Label id=”lblUserName” runat=”server” Text='<%# Container.DataItem(“UserName”) %>’>

</asp:Label>

You can also use the static method Eval() of the DataBinder class for binding data to your controls. This method has an overloaded version that accepts the format expression as an additional parameter that relates to the type of formatting that you would require on the data to be displayed. The syntax for using the Eval() method is shown as follows:

<%# DataBinder.Eval(Container.DataItem, “expression”[, “format”]) %>

As shown in the code snippet the Eval() method accepts two parameters:

  • The first of these parameters is the data container, that is, a data table, a data set or a data view.
  • The second parameter is a reference to the value that needs to be assigned to the control.

Refer to the following code snippet that illustrates, how you can use the DataBinder.Eval() method to bind data:

<asp:Label runat=”server” Text='<%# DataBinder.Eval(Container.DataItem, “UserName”) %>’> </asp:Label>

You can use the overloaded version of the Eval() method to specify the format expression (as an additional optional parameter) to display the data in your required format. Refer to the following code snippet:

<asp:Label id=”lblLoginDate” runat=”server”

Text='<%# DataBinder.Eval(Container.DataItem, “LoginDate”, “{0:dddd d MMMM”]) %>’>

</asp:Label>

This code would display the LoginDate in the label control named lblLoginDate as Sunday 15, April.

Advertisements

One Response to “Data Binding in ASP.NET”

  1. pijpen said

    “It should be a class,” he says.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

 
%d bloggers like this: